import { feature } from 'topojson-client';
import worldData from 'world-atlas/countries-50m.json';

// Full name → alpha-2 map covering all world-atlas country names.
const nameToAlpha2 = {
  'Afghanistan':'AF','Albania':'AL','Algeria':'DZ','American Samoa':'AS',
  'Andorra':'AD','Angola':'AO','Anguilla':'AI','Antigua and Barb.':'AG',
  'Argentina':'AR','Armenia':'AM','Aruba':'AW','Ashmore and Cartier Is.':'AU',
  'Australia':'AU','Austria':'AT','Azerbaijan':'AZ','Bahamas':'BS',
  'Bahrain':'BH','Bangladesh':'BD','Barbados':'BB','Belarus':'BY',
  'Belgium':'BE','Belize':'BZ','Benin':'BJ','Bermuda':'BM','Bhutan':'BT',
  'Bolivia':'BO','Bosnia and Herz.':'BA','Botswana':'BW',
  'Br. Indian Ocean Ter.':'IO','Brazil':'BR','British Virgin Is.':'VG',
  'Brunei':'BN','Bulgaria':'BG','Burkina Faso':'BF','Burundi':'BI',
  'Cabo Verde':'CV','Cambodia':'KH','Cameroon':'CM','Canada':'CA',
  'Cayman Is.':'KY','Central African Rep.':'CF','Chad':'TD','Chile':'CL',
  'China':'CN','Colombia':'CO','Comoros':'KM','Congo':'CG','Cook Is.':'CK',
  'Costa Rica':'CR','Croatia':'HR','Cuba':'CU','Curaçao':'CW','Cyprus':'CY',
  'Czechia':'CZ',"Côte d'Ivoire":'CI','Dem. Rep. Congo':'CD','Denmark':'DK',
  'Djibouti':'DJ','Dominica':'DM','Dominican Rep.':'DO','Ecuador':'EC',
  'Egypt':'EG','El Salvador':'SV','Eq. Guinea':'GQ','Eritrea':'ER',
  'Estonia':'EE','Ethiopia':'ET','Faeroe Is.':'FO','Falkland Is.':'FK',
  'Fiji':'FJ','Finland':'FI','Fr. Polynesia':'PF','France':'FR','Gabon':'GA',
  'Gambia':'GM','Georgia':'GE','Germany':'DE','Ghana':'GH','Greece':'GR',
  'Greenland':'GL','Grenada':'GD','Guam':'GU','Guatemala':'GT',
  'Guernsey':'GG','Guinea':'GN','Guinea-Bissau':'GW','Guyana':'GY',
  'Haiti':'HT','Honduras':'HN','Hong Kong':'HK','Hungary':'HU','Iceland':'IS',
  'India':'IN','Indonesia':'ID','Iran':'IR','Iraq':'IQ','Ireland':'IE',
  'Isle of Man':'IM','Israel':'IL','Italy':'IT','Jamaica':'JM','Japan':'JP',
  'Jersey':'JE','Jordan':'JO','Kazakhstan':'KZ','Kenya':'KE','Kiribati':'KI',
  'Kosovo':'XK','Kuwait':'KW','Kyrgyzstan':'KG','Laos':'LA','Latvia':'LV',
  'Lebanon':'LB','Lesotho':'LS','Liberia':'LR','Libya':'LY',
  'Liechtenstein':'LI','Lithuania':'LT','Luxembourg':'LU','Macao':'MO',
  'Macedonia':'MK','Madagascar':'MG','Malawi':'MW','Malaysia':'MY',
  'Maldives':'MV','Mali':'ML','Malta':'MT','Marshall Is.':'MH',
  'Mauritania':'MR','Mauritius':'MU','Mexico':'MX','Micronesia':'FM',
  'Moldova':'MD','Monaco':'MC','Mongolia':'MN','Montenegro':'ME',
  'Montserrat':'MS','Morocco':'MA','Mozambique':'MZ','Myanmar':'MM',
  'N. Cyprus':'CY','N. Mariana Is.':'MP','Namibia':'NA','Nauru':'NR',
  'Nepal':'NP','Netherlands':'NL','New Caledonia':'NC','New Zealand':'NZ',
  'Nicaragua':'NI','Niger':'NE','Nigeria':'NG','Niue':'NU',
  'Norfolk Island':'NF','North Korea':'KP','Norway':'NO','Oman':'OM',
  'Pakistan':'PK','Palau':'PW','Palestine':'PS','Panama':'PA',
  'Papua New Guinea':'PG','Paraguay':'PY','Peru':'PE','Philippines':'PH',
  'Pitcairn Is.':'PN','Poland':'PL','Portugal':'PT','Puerto Rico':'PR',
  'Qatar':'QA','Romania':'RO','Russia':'RU','Rwanda':'RW','S. Sudan':'SS',
  'Saint Helena':'SH','Saint Lucia':'LC','Samoa':'WS','San Marino':'SM',
  'Saudi Arabia':'SA','Senegal':'SN','Serbia':'RS','Seychelles':'SC',
  'Sierra Leone':'SL','Singapore':'SG','Sint Maarten':'SX','Slovakia':'SK',
  'Slovenia':'SI','Solomon Is.':'SB','Somalia':'SO','South Africa':'ZA',
  'South Korea':'KR','Spain':'ES','Sri Lanka':'LK','St-Barthélemy':'BL',
  'St-Martin':'MF','St. Kitts and Nevis':'KN','St. Pierre and Miquelon':'PM',
  'St. Vin. and Gren.':'VC','Sudan':'SD','Suriname':'SR','Sweden':'SE',
  'Switzerland':'CH','Syria':'SY','São Tomé and Principe':'ST','Taiwan':'TW',
  'Tajikistan':'TJ','Tanzania':'TZ','Thailand':'TH','Timor-Leste':'TL',
  'Togo':'TG','Tonga':'TO','Trinidad and Tobago':'TT','Tunisia':'TN',
  'Turkey':'TR','Turkmenistan':'TM','Turks and Caicos Is.':'TC',
  'U.S. Virgin Is.':'VI','Uganda':'UG','Ukraine':'UA',
  'United Arab Emirates':'AE','United Kingdom':'GB',
  'United States of America':'US','Uruguay':'UY','Uzbekistan':'UZ',
  'Vanuatu':'VU','Vatican':'VA','Venezuela':'VE','Vietnam':'VN',
  'W. Sahara':'EH','Yemen':'YE','Zambia':'ZM','Zimbabwe':'ZW',
  'eSwatini':'SZ','Åland':'AX',
};

const _features = feature(worldData, worldData.objects.countries).features;

export const countryNames = _features
  .map(f => f.properties?.name).filter(Boolean).sort();

// country name → topojson numeric ID (e.g. 'Japan' → '392')
export const nameToId = Object.fromEntries(
  _features
    .filter(f => f.properties?.name && f.id)
    .map(f => [f.properties.name, String(f.id)])
);
nameToId['Kosovo'] = 'XK';

/** @param {string} country */
export function flagEmoji(country) {
  const code = nameToAlpha2[country];
  if (!code) return '';
  return [...code].map(c => String.fromCodePoint(0x1F1E6 - 65 + c.charCodeAt(0))).join('');
}
