class Generators::Avo::Js::InstallGenerator

def create_files

def create_files
  case options[:bundler].to_s
  when "importmap"
    install_for_importmap
  when "esbuild"
    install_for_esbuild
  else
    say "We don't know how to install Avo JS for this bundler \"#{options[:bundler]}\""
  end
end

def install_for_esbuild

def install_for_esbuild
  unless Rails.root.join("app", "javascript", "avo.custom.js").exist?
    say "Add default app/javascript/avo.custom.js"
    copy_file template_path("avo.custom.js"), "app/javascript/avo.custom.js"
  end
  say "Ejecting the _head.html.erb partial"
  Rails::Generators.invoke("avo:eject", [":head", "--skip-avo-version"], {destination_root: Rails.root})
  say "Adding the JS asset to the partial"
  append_to_file Rails.root.join("app", "views", "avo", "partials", "_head.html.erb"), "<%= javascript_include_tag \"avo.custom\", \"data-turbo-track\": \"reload\", defer: true %>"
end

def install_for_importmap

def install_for_importmap
  unless Rails.root.join("app", "javascript", "avo.custom.js").exist?
    say "Add default app/javascript/avo.custom.js"
    copy_file template_path("avo.custom.js"), "app/javascript/avo.custom.js"
  end
  say "Ejecting the _head.html.erb partial"
  Rails::Generators.invoke("avo:eject", [":head", "--skip-avo-version"], {destination_root: Rails.root})
  say "Adding the JS asset to the partial"
  append_to_file Rails.root.join("app", "views", "avo", "partials", "_head.html.erb"), "<%= javascript_importmap_tags \"avo.custom\" %>"
  # pin to importmap
  say "Pin the new entrypoint to your importmap config"
  append_to_file Rails.root.join("config", "importmap.rb"), "\n# Avo custom JS entrypoint\npin \"avo.custom\", preload: true\n"
end

def template_path(filename)

def template_path(filename)
  Pathname.new(__dir__).join("..", "templates", "js", filename).to_s
end