/**
 * Website languages.
 *
 *  - English is the main language and keeps the plain URLs (/features/, /pricing/ …).
 *  - Every other language lives under its own prefix: /es/features/, /es/pricing/ …
 *  - One page file builds every language: pages in src/pages/[...lang]/ export
 *    `getStaticPaths = langPaths`, then read the language with `langFromUrl(Astro.url)`.
 *  - Components pick their text with `pick(lang, { en: …, es: … })` (text kept next to the markup it belongs to);
 *    words used on many pages are in ./ui.ts.
 *
 * To add a language: add it to LANGS below, add its words in ./ui.ts and in each `pick(…)` (TypeScript lists every
 * place that is missing it), then run `npm test`.
 */
export const LANGS = {
  en: { code: 'en', name: 'English', short: 'EN', hreflang: 'en-US', og: 'en_US', intl: 'en-US' },
  es: { code: 'es', name: 'Español', short: 'ES', hreflang: 'es-US', og: 'es_US', intl: 'es-US' },
} as const;

export type Lang = keyof typeof LANGS;
export const DEFAULT_LANG: Lang = 'en';
export const LANG_CODES = Object.keys(LANGS) as Lang[];
/** languages that have a URL prefix (all except English) */
export const PREFIXED = LANG_CODES.filter((l) => l !== DEFAULT_LANG);

const PREFIX_RE = new RegExp(`^/(${PREFIXED.join('|')})(?=/|$)`);

/** language of a page, from its URL */
export function langFromUrl(url: URL | string): Lang {
  const path = typeof url === 'string' ? url : url.pathname;
  const m = path.match(PREFIX_RE);
  return (m ? m[1] : DEFAULT_LANG) as Lang;
}

/** '/es/pricing/' → '/pricing/' */
export function stripLang(path: string): string {
  return path.replace(PREFIX_RE, '') || '/';
}

/**
 * A site link in the given language: localize('/pricing/#faq', 'es') → '/es/pricing/#faq'.
 * External links, mailto:, tel:, files (/rss.xml, /videos/…) and English-only paths are returned unchanged.
 */
export function localize(href: string, lang: Lang): string {
  if (lang === DEFAULT_LANG || !href.startsWith('/') || href.startsWith('//')) return href;
  const path = href.split(/[?#]/)[0];
  if (/\.[a-z0-9]+$/i.test(path) || ENGLISH_ONLY.some((re) => re.test(path))) return href;
  if (PREFIX_RE.test(href)) return href;
  return `/${lang}${href === '/' ? '/' : href}`;
}

/** pages that exist in English only (blog articles come from the CMS in English) */
export const ENGLISH_ONLY = [/^\/blog\/[^/]+\/?$/];

/** for getStaticPaths in src/pages/[...lang]/: one copy of the page per language */
export const langPaths = () => LANG_CODES.map((l) => ({ params: { lang: l === DEFAULT_LANG ? undefined : l } }));

/** text for this language: pick(lang, { en: 'Pricing', es: 'Precios' }) */
export const pick = <T>(lang: Lang, byLang: Record<Lang, T>): T => byLang[lang] ?? byLang[DEFAULT_LANG];

/** the same page in every language (for hreflang and the language menu) */
export function alternates(pathname: string, langs: readonly Lang[] = LANG_CODES) {
  const base = stripLang(pathname);
  return langs.map((l) => ({ lang: l, ...LANGS[l], href: localize(base, l) }));
}

/** number / date / money in the page language (US formats for both: $1,234.50, 11/17/2026 → "17 de noviembre de 2026") */
export const intl = (lang: Lang) => LANGS[lang].intl;
