module Tailwindcss::Commands

def compile_command(debug: false, **kwargs)

def compile_command(debug: false, **kwargs)
  command = [
    executable(**kwargs),
    "-i", Rails.root.join("app/assets/stylesheets/application.tailwind.css").to_s,
    "-o", Rails.root.join("app/assets/builds/tailwind.css").to_s,
    "-c", Rails.root.join("config/tailwind.config.js").to_s,
  ]
  command << "--minify" unless (debug || rails_css_compressor?)
  postcss_path = Rails.root.join("config/postcss.config.js")
  command += ["--postcss", postcss_path.to_s] if File.exist?(postcss_path)
  command
end

def executable(exe_path: DEFAULT_DIR)

def executable(exe_path: DEFAULT_DIR)
  tailwindcss_install_dir = ENV["TAILWINDCSS_INSTALL_DIR"]
  if tailwindcss_install_dir
    if File.directory?(tailwindcss_install_dir)
      warn "NOTE: using TAILWINDCSS_INSTALL_DIR to find tailwindcss executable: #{tailwindcss_install_dir}"
      exe_path = tailwindcss_install_dir
      exe_file = File.expand_path(File.join(tailwindcss_install_dir, "tailwindcss"))
    else
      raise DirectoryNotFoundException, <<~MESSAGE
        TAILWINDCSS_INSTALL_DIR is set to #{tailwindcss_install_dir}, but that directory does not exist.
      MESSAGE
    end
  else
    if Tailwindcss::Upstream::NATIVE_PLATFORMS.keys.none? { |p| Gem::Platform.match_gem?(Gem::Platform.new(p), GEM_NAME) }
      raise UnsupportedPlatformException, <<~MESSAGE
        tailwindcss-rails does not support the #{platform} platform
        Please install tailwindcss following instructions at https://tailwindcss.com/docs/installation
      MESSAGE
    end
    exe_file = Dir.glob(File.expand_path(File.join(exe_path, "*", "tailwindcss"))).find do |f|
      Gem::Platform.match_gem?(Gem::Platform.new(File.basename(File.dirname(f))), GEM_NAME)
    end
  end
  if exe_file.nil? || !File.exist?(exe_file)
    raise ExecutableNotFoundException, <<~MESSAGE
      Cannot find the tailwindcss executable for #{platform} in #{exe_path}
      If you're using bundler, please make sure you're on the latest bundler version:
          gem install bundler
          bundle update --bundler
      Then make sure your lock file includes this platform by running:
          bundle lock --add-platform #{platform}
          bundle install
      See `bundle lock --help` output for details.
      If you're still seeing this message after taking those steps, try running
      `bundle config` and ensure `force_ruby_platform` isn't set to `true`. See
      https://github.com/rails/tailwindcss-rails#check-bundle_force_ruby_platform
      for more details.
    MESSAGE
  end
  exe_file
end

def platform

def platform
  [:cpu, :os].map { |m| Gem::Platform.local.send(m) }.join("-")
end

def rails_css_compressor?

def rails_css_compressor?
  defined?(Rails) && Rails&.application&.config&.assets&.css_compressor.present?
end

def watch_command(always: false, poll: false, **kwargs)

def watch_command(always: false, poll: false, **kwargs)
  compile_command(**kwargs).tap do |command|
    command << "-w"
    command << "always" if always
    command << "-p" if poll
  end
end