class CKEditor5::Rails::Assets::AssetsBundleHtmlSerializer

def self.url_resource_preload_type(url)

def self.url_resource_preload_type(url)
  case File.extname(url)
  when '.js' then 'script'
  when '.css' then 'style'
  else 'fetch'
  end
end

def initialize(bundle, importmap: true, lazy: false)

def initialize(bundle, importmap: true, lazy: false)
  raise TypeError, 'bundle must be an instance of AssetsBundle' unless bundle.is_a?(AssetsBundle)
  @importmap = importmap
  @bundle = bundle
  @lazy = lazy
end

def preload_tags(nonce: nil)

def preload_tags(nonce: nil)
  preloads = bundle.preloads.map do |preload|
    if preload.is_a?(Hash) && preload[:as] && preload[:href]
      tag.link(
        **preload,
        nonce: nonce,
        crossorigin: 'anonymous'
      )
    else
      tag.link(
        href: preload,
        rel: 'preload',
        nonce: nonce,
        as: self.class.url_resource_preload_type(preload),
        crossorigin: 'anonymous'
      )
    end
  end
  safe_join(preloads)
end

def styles_tags(nonce: nil)

def styles_tags(nonce: nil)
  styles = bundle.stylesheets.map do |url|
    tag.link(href: url, nonce: nonce, rel: 'stylesheet', crossorigin: 'anonymous')
  end
  safe_join(styles)
end

def to_html(nonce: nil)

def to_html(nonce: nil)
  tags = [
    WebComponentBundle.instance.to_html(nonce: nonce)
  ]
  unless lazy
    tags.prepend(
      preload_tags(nonce: nonce),
      styles_tags(nonce: nonce),
      window_scripts_tags(nonce: nonce)
    )
  end
  if importmap
    tags.prepend(
      AssetsImportMap.new(bundle).to_html(nonce: nonce)
    )
  end
  safe_join(tags)
end

def window_scripts_tags(nonce: nil)

def window_scripts_tags(nonce: nil)
  scripts = bundle.scripts.filter_map do |script|
    tag.script(src: script.url, nonce: nonce, crossorigin: 'anonymous') if script.window?
  end
  safe_join(scripts)
end