export const locales = ["en", "pl"] as const;
export type Locale = (typeof locales)[number];
export const defaultLocale: Locale = "en";

export function isLocale(value: string): value is Locale {
  return (locales as readonly string[]).includes(value);
}

/**
 * Very small header-based negotiator used by middleware to pick a default
 * locale from the browser's Accept-Language header. We deliberately avoid
 * a heavy dependency here — Tidyly only supports two locales.
 */
export function pickLocaleFromAcceptLanguage(header: string | null): Locale {
  if (!header) return defaultLocale;
  const preferred = header
    .split(",")
    .map((part) => part.split(";")[0].trim().toLowerCase());

  for (const lang of preferred) {
    if (lang.startsWith("pl")) return "pl";
    if (lang.startsWith("en")) return "en";
  }
  return defaultLocale;
}
