# typed: false
module Domain::Posts::E621PostsHelper
  def icon_asset_for_url(url)
    domain = extract_domain(url)
    return nil unless domain

    domain_patterns = {
      %w[*.e621.net e621.net] => "e621.png",
      %w[*.furaffinity.net furaffinity.net] => "fa.png",
      %w[*.bsky.app bsky.app] => "bsky.png",
      %w[*.itaku.ee itaku.ee] => "itaku.png",
      %w[*.deviantart.com deviantart.com *.wixmp.com] => "deviantart.png",
      %w[*.twitter.com twitter.com *.x.com x.com] => "x-twitter.png",
      %w[*.inkbunny.net inkbunny.net *.ib.metapix.net ib.metapix.net] =>
        "inkbunny.png",
      %w[*.newgrounds.com newgrounds.com] => "newgrounds.png",
      %w[*.patreon.com patreon.com] => "patreon.png",
      %w[*.pixiv.net pixiv.net *.pximg.net pximg.net] => "pixiv.png",
    }

    domain_patterns.each do |patterns, icon|
      patterns.each do |pattern|
        if File.fnmatch?(pattern, domain, File::FNM_PATHNAME)
          return asset_path("domain-icons/#{icon}")
        end
      end
    end
    nil
  end

  def tag_category_tw_class(category)
    case category.to_sym
    when :general
      "bg-blue-300" # Light blue
    when :artist
      "bg-indigo-300" # Light indigo
    when :copyright
      "bg-purple-300" # Light purple
    when :character
      "bg-green-300" # Light green
    when :species
      "bg-teal-300" # Light teal
    when :invalid
      "bg-slate-300" # Medium gray
    when :meta
      "bg-amber-300" # Light amber
    when :lore
      "bg-cyan-300" # Light cyan
    else
      "bg-white" # White (default)
    end
  end

  def tag_category_order
    %i[artist copyright character species general meta lore invalid]
  end

  def font_awesome_category_icon(category)
    case category.to_sym
    when :artist
      "fa-brush"
    when :species
      "fa-paw"
    when :character
      "fa-user"
    when :copyright
      "fa-copyright"
    when :general
      "fa-tag"
    when :lore
      "fa-book"
    when :meta
      "fa-info"
    when :invalid
      "fa-ban"
    end
  end

  def fa_post_for_source(source)
    uri = URI.parse(source)
    return unless %w[www.furaffinity.net furaffinity.net].include?(uri.host)
    fa_id = uri.path.match(%r{/view/(\d+)})[1]
    return unless fa_id
    raise("not implemented")
  rescue StandardError
    nil
  end

  private

  def extract_domain(url)
    URI.parse(url).host
  rescue URI::InvalidURIError
    nil
  end
end
