# typed: strict
class Scraper::FaHttpClientConfig < Scraper::HttpClientConfig
  DEFAULT_ALLOWED_DOMAINS = %w[*.furaffinity.net *.facdn.net ipinfo.io]
  UUID_PATTERN =
    /\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/i
  OAID_PATTERN = /\A[0-9a-f]{32}\z/i

  sig { override.returns(T.nilable(T::Array[T.untyped])) }
  def cookies
    a_cookie = GlobalState.get("furaffinity-cookie-a")
    b_cookie = GlobalState.get("furaffinity-cookie-b")
    oaid_cookie = GlobalState.get("furaffinity-cookie-oaid")

    validate_cookie!("a", a_cookie, UUID_PATTERN)
    validate_cookie!("b", b_cookie, UUID_PATTERN)
    validate_cookie!("OAID", oaid_cookie, OAID_PATTERN)

    [
      {
        domain: ".furaffinity.net",
        cookies: [
          { name: "a", value: a_cookie, path: "/" },
          { name: "b", value: b_cookie, path: "/" },
        ],
      },
      {
        domain: "rv.furaffinity.net",
        cookies: [{ name: "OAID", value: oaid_cookie, path: "/" }],
      },
    ]
  end

  sig { override.returns(T::Array[[String, Numeric]]) }
  def ratelimit
    # number represents minimum delay in seconds between requests to the same domain
    [["d.furaffinity.net", 1.0], ["*.facdn.net", 1.0], ["*", 1.5]]
  end

  sig { override.returns(T::Array[String]) }
  def allowed_domains
    DEFAULT_ALLOWED_DOMAINS
  end

  sig { override.returns(Integer) }
  def redirect_limit
    4
  end

  private

  sig { params(name: String, value: T.nilable(String), pattern: Regexp).void }
  def validate_cookie!(name, value, pattern)
    raise "#{name} cookie is not set" if value.nil? || value.empty?
    raise "#{name} cookie has invalid format" unless value.match?(pattern)
  end
end
