/**
 * Geo guardrail.
 *
 * The host marketplace serves a configured set of countries; traffic from outside
 * the allow-list is flagged GEO_OUT_OF_RANGE and does not receive a referral cookie.
 *
 * The `allowedCountries` override allows future expansion; defaults to ['IL'].
 */

import type { GuardResult } from './brand-keyword.js'

const DEFAULT_ALLOWED_COUNTRIES = ['IL']

/**
 * Evaluate geo-mismatch.
 *
 * @param country        - ISO 3166-1 alpha-2 country code from CF-IPCountry header.
 * @param allowedCountries - Override allowed list; defaults to ['IL'] when empty.
 */
export function evaluateGeo(country: string, allowedCountries: string[]): GuardResult {
  const allowed = allowedCountries.length > 0 ? allowedCountries : DEFAULT_ALLOWED_COUNTRIES
  const countryUpper = country.toUpperCase()

  if (!countryUpper || !allowed.includes(countryUpper)) {
    return {
      isSuspicious: true,
      codes: ['GEO_OUT_OF_RANGE'],
      setCookie: false,
    }
  }

  return { isSuspicious: false, codes: [], setCookie: true }
}
