import { getData, type CountryRecord } from 'country-list'; export interface CountryOption { value: string; label: string; } const baseCountries = getData(); export const countryOptions: CountryOption[] = baseCountries .map((country: CountryRecord) => ({ value: country.code, label: country.name, })) .sort((a: CountryOption, b: CountryOption) => a.label.localeCompare(b.label)); const countryByCode = new Map( countryOptions.map((country) => [country.value.toUpperCase(), country.label]), ); const countryByName = new Map( countryOptions.map((country) => [country.label.toLowerCase(), country.value]), ); export const resolveCountryCode = (value?: string) => { if (!value) { return ''; } const trimmed = value.trim(); if (!trimmed) { return ''; } const upper = trimmed.toUpperCase(); if (countryByCode.has(upper)) { return upper; } const byName = countryByName.get(trimmed.toLowerCase()); return byName ?? ''; }; export const getCountryLabel = (value?: string) => { if (!value) { return ''; } const resolved = resolveCountryCode(value); if (!resolved) { return value; } return countryByCode.get(resolved) ?? value; };