import * as countryRepository from '../repositories/countryRepository'; const UNKNOWN_COUNTRY_CODE = 'XX'; let countryMapCache: Map | null = null; export async function getCountryNameMap(): Promise> { if (!countryMapCache) { const countries = await countryRepository.listAll(); countryMapCache = new Map(countries.map((c) => [c.code, c.name])); } return countryMapCache; } /** Clears cached country names (e.g. after admin sync). */ export function clearCountryNameCache(): void { countryMapCache = null; } export function formatCountryCode( code: string | null | undefined, map: Map ): string { if (!code) return ''; if (code === UNKNOWN_COUNTRY_CODE) return 'Non renseigné'; return map.get(code) ?? map.get(code.toUpperCase()) ?? code; }