# frozen_string_literal: true

module RedmineUxToolkit
  module Patches
    module ApplicationHelperPatch
      def body_css_classes
        css = super.to_s
        modern_ui = ux_toolkit_modern_ui
        return css unless modern_ui

        modern_ui_classes = modern_ui.body_classes
        return css if modern_ui_classes.empty?

        ([css] + modern_ui_classes).join(' ').strip
      end

      def textilizable(*args)
        rendered = super
        return rendered unless RedmineUxToolkit::Services::ExternalLinksProcessor.enabled?

        processed = RedmineUxToolkit::Services::ExternalLinksProcessor.new(
          rendered,
          ux_toolkit_current_request_host
        ).process
        processed.html_safe
      end

      private

      def ux_toolkit_current_request_host
        return nil unless respond_to?(:request) && request

        request.host
      end
    end
  end
end
