import {
  DEFAULT_LANGUAGE,
  LANGUAGES,
  languageToLocale,
  type Language,
} from "@/i18n/resolver";

export interface HreflangEntry {
  /** Short language code as Google prefers in `hreflang`. */
  lang: Language | "x-default";
  href: string;
}

/**
 * Builds the full set of reciprocal hreflang links for a given path.
 * Returns 9 language entries + 1 x-default entry (pointing to the
 * default language). Hreflang attribute uses the short language code
 * (`hreflang="en"`); the URL itself uses the BCP-47 locale code
 * (`/en-en/...`) to match the customer's URL contract.
 */
export function buildHreflangSet(args: {
  canonicalOrigin: string;
  pathWithoutLocale: string;
}): HreflangEntry[] {
  const { canonicalOrigin, pathWithoutLocale } = args;

  const entries: HreflangEntry[] = LANGUAGES.map((lang) => ({
    lang,
    href: `${canonicalOrigin}/${languageToLocale(lang)}${pathWithoutLocale}`,
  }));

  entries.push({
    lang: "x-default",
    href: `${canonicalOrigin}/${languageToLocale(DEFAULT_LANGUAGE)}${pathWithoutLocale}`,
  });

  return entries;
}
