import { IANA_ISO3166 } from '@/lib/iso3166-iana'; /** ISO 3166-1 alpha-2 → regional indicator symbols (flag emoji). */ export function flagEmojiFromCode(code) { if (!code || typeof code !== 'string' || code.length !== 2) return '🏳️'; const u = code.toUpperCase(); if (u.length !== 2 || /[^A-Z]/.test(u)) return '🏳️'; return String.fromCodePoint(...[...u].map((c) => 127397 + c.charCodeAt(0))); } let cached = null; /** * Full country list: IANA iso3166.tab (every ISO alpha-2 region), display names * upgraded via {@link Intl.DisplayNames} when available. * * @returns {{ code: string, name: string }[]} */ export function getAllCountries() { if (cached) return cached; const dn = typeof Intl !== 'undefined' && typeof Intl.DisplayNames === 'function' ? new Intl.DisplayNames(['en'], { type: 'region' }) : null; cached = IANA_ISO3166.map(({ code, name: ianaName }) => { let name = ianaName; try { if (dn) { const intl = dn.of(code); if (intl && intl !== code) name = intl; } } catch { /* keep IANA label */ } return { code, name }; }).sort((a, b) => a.name.localeCompare(b.name)); return cached; } /** * Match stored deal.country (full name or code) to a row. * @param {string} stored * @param {{ code: string, name: string }[]} countries */ export function matchCountry(stored, countries) { if (!stored || typeof stored !== 'string') return null; const s = stored.trim(); if (!s) return null; const lower = s.toLowerCase(); const byName = countries.find((c) => c.name.toLowerCase() === lower); if (byName) return byName; const byCode = countries.find((c) => c.code.toLowerCase() === lower); if (byCode) return byCode; const iana = IANA_ISO3166.find((c) => c.name.toLowerCase() === lower); if (iana) { return countries.find((c) => c.code === iana.code) || iana; } const fuzzy = countries.find((c) => c.name.toLowerCase().includes(lower)); if (fuzzy) return fuzzy; const ianaFuzzy = IANA_ISO3166.find((c) => c.name.toLowerCase().includes(lower)); if (ianaFuzzy) { return countries.find((c) => c.code === ianaFuzzy.code) || ianaFuzzy; } return null; }