# frozen_string_literal: true

require "ostruct"
require "erb"

module Decidim
  module Assets
    module Tailwind
      class Instance
        def write_runtime_configuration
          evaluated_template = ERB.new(tailwind_configuration_template).result(tailwind_variables.instance_eval { binding })

          File.write(File.join(app_path, "tailwind.config.js"), evaluated_template)
        end

        private

        def tailwind_variables
          # The directories where Tailwind should search for templates are the installed
          # decidim gems and the current directory (which is the Rails app)
          decidim_gems = Bundler.load.specs.select { |spec| spec.name =~ /^decidim-/ }.map(&:full_gem_path)
          content_directories = decidim_gems.push(".")

          # The variable expected by tailwind is a JavaScript array of strings
          # The directory globbing with the star is done in Ruby because it was causing an infinite loop
          # when processed by Tailwind
          content_directories_as_array_of_strings = content_directories.map { |content_directory| "'#{content_directory}'" }.join(",")

          OpenStruct.new(tailwind_content_directories: content_directories_as_array_of_strings)
        end

        def tailwind_configuration_template
          File.read(configuration_template_path)
        end

        def configuration_template_path
          @configuration_template_path ||= begin
            original_path = File.expand_path("tailwind.config.js.erb", __dir__)
            alternate_path = File.expand_path("config/tailwind.config.js.erb", app_path)

            File.exist?(alternate_path) ? alternate_path : original_path
          end
        end

        def app_path
          @app_path ||=
            if defined?(Rails)
              Rails.application.root
            else
              # This is used when Rails is not available from the shakapacker binstubs
              File.expand_path(".", Dir.pwd)
            end
        end
      end
    end
  end
end
