import { getCountries } from 'react-phone-number-input';

/**
 * Liste des pays pour le champ « Pays/Région ».
 * - `label` : nom français (affiché à l'utilisateur)
 * - `value` : nom anglais (stocké côté HubSpot — propriété `country` standard, libellés EN)
 */
export type CountryOption = { iso: string; label: string; value: string };

const display = (locale: string) => {
  try {
    return new Intl.DisplayNames([locale], { type: 'region' });
  } catch {
    return null;
  }
};

const frNames = display('fr');
const enNames = display('en');

const nameOf = (dn: Intl.DisplayNames | null, iso: string): string => {
  try {
    return dn?.of(iso) ?? iso;
  } catch {
    return iso;
  }
};

export const countryOptions: CountryOption[] = getCountries()
  .map((iso) => ({
    iso,
    label: nameOf(frNames, iso),
    value: nameOf(enNames, iso),
  }))
  .sort((a, b) => a.label.localeCompare(b.label, 'fr'));
