module Jets::AssetTagHelper

def add_s3_public(url)

Returns: https://s3-us-west-2.amazonaws.com/demo-dev-s3bucket-1jg5o076egkk4/jets/public/packs/application-e7654c50abd78161b641.js
Url: /packs/application-e7654c50abd78161b641.js
Example:
def add_s3_public(url)
  return url unless on_aws?
  "#{s3_public}#{url}"
end

def asset_folder?(url)

Example: packs, images, assets
Whatever is configured in Jets.config.assets.folders
def asset_folder?(url)
  Jets.config.assets.folders.detect do |folder|
    url.include?(folder)
  end
end

def asset_path(source, options = {})

def asset_path(source, options = {})
  source = source.to_s # convert to String because passing a posts.photo object to results in failure to resolve the immage to a URL since we haven't defined polymorphic_url yet
  # mimic original behavior to get /images in source
  source = "/images/#{source}" unless source.starts_with?('/') || source.starts_with?('http')
  # Examples to help understand:
  #
  #   puts "AssetTagHelper#asset_path source #{source}"
  #   puts "AssetTagHelper#asset_path asset_folder?(source) #{asset_folder?(source).inspect}"
  #   AssetTagHelper#asset_path source /packs/images/myimage-e5f675d1ba26865fd65e919beb5bb86b.png
  #   AssetTagHelper#asset_path asset_folder?(source) "images"
  #
  if on_aws? && asset_folder?(source) && !source.starts_with?('http')
    source = "#{s3_public}#{source}"
  end
  super
end

def favicon_path(path='favicon.ico')

but then that messes up form data.
an Accept header doesnt work well. You can changed Media Types to '*/*'
Useful helper for API Gateway since serving binary data like images without

Serves favicon out of s3 when on API gateway.
def favicon_path(path='favicon.ico')
  on_aws? ? "#{s3_public}/#{path}" : "/#{path}"
end

def image_tag(source, options = {})

image_tag("jets.png") => https://s3-us-west-2.amazonaws.com/demo-dev-s3bucket-1kih4n2te0n66/jets/public/images/jets.png

Remotely:

image_tag("/images/jets.png") => /images/jets.png
image_tag("jets.png") => /images/jets.png

Locally:
def image_tag(source, options = {})
  source = source.to_s # convert to String because passing a posts.photo object to results in failure to resolve the immage to a URL since we haven't defined polymorphic_url yet
  # mimic original behavior to get /images in source
  source = "/images/#{source}" unless source.starts_with?('/') || source.starts_with?('http')
  if on_aws? && !source.starts_with?('http')
    source = "#{s3_public}#{source}"
  end
  super
end

def javascript_include_tag(*sources, **options)

def javascript_include_tag(*sources, **options)
  sources = sources.map { |s| s3_public_url(s, :javascripts) }
  super
end

def s3_public

def s3_public
  # s3_base_url.txt is created as part of the build process
  s3_base_url = IO.read("#{Jets.root}/config/s3_base_url.txt").strip
  "#{s3_base_url}/public"
end

def s3_public_url(url, asset_type)

/ in front.
So we can add the javascript ourselves and then add the stag with a
If there's a / in front then rails will not add the "javascript":

/javascript/stag/asset/test

But adding it in front results in this:

/stag/javascript/asset/test

We want to add the API Gateway stage name in front for this:

/javascripts/assets/test

Rails automatically adds "javscript" in front, to become:

javascript_include_tag "assets/test"
User can use:
def s3_public_url(url, asset_type)
  unless url.starts_with?('/') or url.starts_with?('http')
    url = "/#{asset_type}/#{url}" # /javascript/asset/test
  end
  add_s3_public(url)
end

def stylesheet_link_tag(*sources, **options)

def stylesheet_link_tag(*sources, **options)
  sources = sources.map { |s| s3_public_url(s, :stylesheets) }
  super
end