/** * Country data — single source of truth on the client. * * The canonical list lives in PHP at `Yatra\Helpers\FormatHelper::getCountries()` * (full ISO-3166-1 alpha-2 + commonly-accepted territories). The backend * localizes it into `window.yatraAdmin.countries` on every admin page * load, so this module's job is just to: * * 1. Read from that localized payload (zero network round-trip) * 2. Offer the two shapes the React code happens to want — a * `{code => name}` map (what the canonical PHP returns) and an * `Array<{code, name}>` (what dropdown components prefer) * 3. Provide a tiny code→name lookup helper for badge/label render * * If the localized payload is missing (very early bootstrap, tests, * etc.) we fall back to a minimal hardcoded set so the UI doesn't * crash. That fallback is intentionally tiny — production should * always hit the real localized list. * * Operators that want a custom order / curated subset apply the * `yatra_countries_list` PHP filter once; the change propagates here * automatically on the next page load. */ export type CountryMap = Record; export interface CountryOption { code: string; name: string; } const FALLBACK_COUNTRIES: CountryMap = { US: "United States", GB: "United Kingdom", CA: "Canada", AU: "Australia", IN: "India", NP: "Nepal", }; /** * Return the canonical `{CODE: 'Name'}` map. Reads from the localized * payload first; falls back to the minimal set when missing. */ export function getCountryMap(): CountryMap { const raw = (window as any)?.yatraAdmin?.countries; if (raw && typeof raw === "object" && !Array.isArray(raw)) { const map = raw as CountryMap; // Guard against an accidentally-empty object from the server. if (Object.keys(map).length > 0) return map; } return FALLBACK_COUNTRIES; } /** * Return countries as an array of `{code, name}` — the shape dropdown * components and `