package SL::Helper::ISO3166;

use strict;
use warnings;
use utf8;

use Exporter qw(import);
our @EXPORT_OK = qw(map_name_to_alpha_2_code);

use List::Util qw(first);

my @alpha_2_mappings = (
  [ 'AD', qr{^(?:AD|Andorra)$}i ],
  [ 'AE', qr{^(?:AE|United Arab Emirates|Vereinigte Arabische Emirate)$}i ],
  [ 'AF', qr{^(?:AF|Afghanistan)$}i ],
  [ 'AG', qr{^(?:AG|Antigua and Barbuda|Antigua und Barbuda)$}i ],
  [ 'AI', qr{^(?:AI|Anguilla)$}i ],
  [ 'AL', qr{^(?:AL|Albania|Albanien)$}i ],
  [ 'AM', qr{^(?:AM|Armenia|Armenien)$}i ],
  [ 'AO', qr{^(?:AO|Angola)$}i ],
  [ 'AQ', qr{^(?:AQ|Antarctica|Antarktis)$}i ],
  [ 'AR', qr{^(?:AR|Argentina|Argentinien)$}i ],
  [ 'AS', qr{^(?:AS|American Samoa|Amerikanisch-Samoa)$}i ],
  [ 'AT', qr{^(?:AT|A|Austria|Österreich)$}i ],
  [ 'AU', qr{^(?:AU|Australia|Australien)$}i ],
  [ 'AW', qr{^(?:AW|Aruba)$}i ],
  [ 'AX', qr{^(?:AX|Åland Islands|Åland)$}i ],
  [ 'AZ', qr{^(?:AZ|Azerbaijan|Aserbaidschan)$}i ],
  [ 'BA', qr{^(?:BA|Bosnia and Herzegovina|Bosnien und Herzegowina)$}i ],
  [ 'BB', qr{^(?:BB|Barbados)$}i ],
  [ 'BD', qr{^(?:BD|Bangladesh|Bangladesch)$}i ],
  [ 'BE', qr{^(?:BE|Belgium|Belgien)$}i ],
  [ 'BF', qr{^(?:BF|Burkina Faso)$}i ],
  [ 'BG', qr{^(?:BG|Bulgaria|Bulgarien)$}i ],
  [ 'BH', qr{^(?:BH|Bahrain)$}i ],
  [ 'BI', qr{^(?:BI|Burundi)$}i ],
  [ 'BJ', qr{^(?:BJ|Benin)$}i ],
  [ 'BL', qr{^(?:BL|Saint Barthélemy|Saint-Barthélemy)$}i ],
  [ 'BM', qr{^(?:BM|Bermuda)$}i ],
  [ 'BN', qr{^(?:BN|Brunei Darussalam|Brunei)$}i ],
  [ 'BO', qr{^(?:BO|Bolivia \(Plurinational State of\)|Bolivien)$}i ],
  [ 'BQ', qr{^(?:BQ|Bonaire, Sint Eustatius and Saba)$}i ],
  [ 'BR', qr{^(?:BR|Brazil|Brasilien)$}i ],
  [ 'BS', qr{^(?:BS|Bahamas)$}i ],
  [ 'BT', qr{^(?:BT|Bhutan)$}i ],
  [ 'BV', qr{^(?:BV|Bouvet Island|Bouvetinsel)$}i ],
  [ 'BW', qr{^(?:BW|Botswana)$}i ],
  [ 'BY', qr{^(?:BY|Belarus)$}i ],
  [ 'BZ', qr{^(?:BZ|Belize)$}i ],
  [ 'CA', qr{^(?:CA|Canada|Kanada)$}i ],
  [ 'CC', qr{^(?:CC|Cocos \(Keeling\) Islands|Cocos Islands|Keeling Islands|Kokosinseln)$}i ],
  [ 'CD', qr{^(?:CD|Congo, Democratic Republic of the|Demokratische Republik Kongo)$}i ],
  [ 'CF', qr{^(?:CF|Central African Republic|Zentralafrikanische Republik)$}i ],
  [ 'CG', qr{^(?:CG|Congo|Republik Kongo)$}i ],
  [ 'CH', qr{^(?:CH|Switzerland|Schweiz)$}i ],
  [ 'CI', qr{^(?:CI|Côte d'Ivoire|Elfenbeinküste)$}i ],
  [ 'CK', qr{^(?:CK|Cook Islands|Cookinseln)$}i ],
  [ 'CL', qr{^(?:CL|Chile)$}i ],
  [ 'CM', qr{^(?:CM|Cameroon|Kamerun)$}i ],
  [ 'CN', qr{^(?:CN|China|Volksrepublik China)$}i ],
  [ 'CO', qr{^(?:CO|Colombia|Kolumbien)$}i ],
  [ 'CR', qr{^(?:CR|Costa Rica)$}i ],
  [ 'CU', qr{^(?:CU|Cuba|Kuba)$}i ],
  [ 'CV', qr{^(?:CV|Cabo Verde|Kap Verde)$}i ],
  [ 'CW', qr{^(?:CW|Curaçao)$}i ],
  [ 'CX', qr{^(?:CX|Christmas Island|Weihnachtsinsel)$}i ],
  [ 'CY', qr{^(?:CY|Cyprus|Zypern)$}i ],
  [ 'CZ', qr{^(?:CZ|Czechia|Tschechien)$}i ],
  [ 'DE', qr{^(?:DE|Germany|D|Deutschland)$}i ],
  [ 'DJ', qr{^(?:DJ|Djibouti|Dschibuti)$}i ],
  [ 'DK', qr{^(?:DK|Denmark|Dänemark)$}i ],
  [ 'DM', qr{^(?:DM|Dominica)$}i ],
  [ 'DO', qr{^(?:DO|Dominican Republic|Dominikanische Republik)$}i ],
  [ 'DZ', qr{^(?:DZ|Algeria|Algerien)$}i ],
  [ 'EC', qr{^(?:EC|Ecuador)$}i ],
  [ 'EE', qr{^(?:EE|Estonia|Estland)$}i ],
  [ 'EG', qr{^(?:EG|Egypt|Ägypten)$}i ],
  [ 'EH', qr{^(?:EH|Western Sahara|Westsahara)$}i ],
  [ 'ER', qr{^(?:ER|Eritrea)$}i ],
  [ 'ES', qr{^(?:ES|Spain|Spanien)$}i ],
  [ 'ET', qr{^(?:ET|Ethiopia|Äthiopien)$}i ],
  [ 'FI', qr{^(?:FI|Finland|Finnland)$}i ],
  [ 'FJ', qr{^(?:FJ|Fiji|Fidschi)$}i ],
  [ 'FK', qr{^(?:FK|Falkland Islands \(Malvinas\)|Falkland Islands|Falklands|Falklandinseln)$}i ],
  [ 'FM', qr{^(?:FM|Micronesia \(Federated States of\)|Micronesia|Föderierte Staaten von Mikronesien)$}i ],
  [ 'FO', qr{^(?:FO|Faroe Islands|Färöer)$}i ],
  [ 'FR', qr{^(?:FR|France|Frankreich)$}i ],
  [ 'GA', qr{^(?:GA|Gabon|Gabun)$}i ],
  [ 'GB', qr{^(?:GB|United Kingdom of Great Britain and Northern Ireland|Vereinigtes Königreich)$}i ],
  [ 'GD', qr{^(?:GD|Grenada)$}i ],
  [ 'GE', qr{^(?:GE|Georgia|Georgien)$}i ],
  [ 'GF', qr{^(?:GF|French Guiana|Französisch-Guayana)$}i ],
  [ 'GG', qr{^(?:GG|Guernsey)$}i ],
  [ 'GH', qr{^(?:GH|Ghana)$}i ],
  [ 'GI', qr{^(?:GI|Gibraltar)$}i ],
  [ 'GL', qr{^(?:GL|Greenland|Grönland)$}i ],
  [ 'GM', qr{^(?:GM|Gambia)$}i ],
  [ 'GN', qr{^(?:GN|Guinea)$}i ],
  [ 'GP', qr{^(?:GP|Guadeloupe)$}i ],
  [ 'GQ', qr{^(?:GQ|Equatorial Guinea|Äquatorialguinea)$}i ],
  [ 'GR', qr{^(?:GR|Greece|Griechenland)$}i ],
  [ 'GS', qr{^(?:GS|South Georgia and the South Sandwich Islands|Südgeorgien und die Südlichen Sandwichinseln)$}i ],
  [ 'GT', qr{^(?:GT|Guatemala)$}i ],
  [ 'GU', qr{^(?:GU|Guam)$}i ],
  [ 'GW', qr{^(?:GW|Guinea-Bissau)$}i ],
  [ 'GY', qr{^(?:GY|Guyana)$}i ],
  [ 'HK', qr{^(?:HK|Hong Kong|Hongkong)$}i ],
  [ 'HM', qr{^(?:HM|Heard Island and McDonald Islands|Heard und McDonaldinseln)$}i ],
  [ 'HN', qr{^(?:HN|Honduras)$}i ],
  [ 'HR', qr{^(?:HR|Croatia|Kroatien)$}i ],
  [ 'HT', qr{^(?:HT|Haiti)$}i ],
  [ 'HU', qr{^(?:HU|Hungary|Ungarn)$}i ],
  [ 'ID', qr{^(?:ID|Indonesia|Indonesien)$}i ],
  [ 'IE', qr{^(?:IE|Ireland|Irland)$}i ],
  [ 'IL', qr{^(?:IL|Israel)$}i ],
  [ 'IM', qr{^(?:IM|Isle of Man)$}i ],
  [ 'IN', qr{^(?:IN|India|Indien)$}i ],
  [ 'IO', qr{^(?:IO|British Indian Ocean Territory|Britisches Territorium im Indischen Ozean)$}i ],
  [ 'IQ', qr{^(?:IQ|Iraq|Irak)$}i ],
  [ 'IR', qr{^(?:IR|Iran \(Islamic Republic of\)|Iran)$}i ],
  [ 'IS', qr{^(?:IS|Iceland|Island)$}i ],
  [ 'IT', qr{^(?:IT|Italy|Italien)$}i ],
  [ 'JE', qr{^(?:JE|Jersey)$}i ],
  [ 'JM', qr{^(?:JM|Jamaica|Jamaika)$}i ],
  [ 'JO', qr{^(?:JO|Jordan|Jordanien)$}i ],
  [ 'JP', qr{^(?:JP|Japan)$}i ],
  [ 'KE', qr{^(?:KE|Kenya|Kenia)$}i ],
  [ 'KG', qr{^(?:KG|Kyrgyzstan|Kirgisistan)$}i ],
  [ 'KH', qr{^(?:KH|Cambodia|Kambodscha)$}i ],
  [ 'KI', qr{^(?:KI|Kiribati)$}i ],
  [ 'KM', qr{^(?:KM|Comoros|Komoren)$}i ],
  [ 'KN', qr{^(?:KN|Saint Kitts and Nevis|St\. Kitts und Nevis)$}i ],
  [ 'KP', qr{^(?:KP|Korea \(Democratic People's Republic of\)|Nordkorea)$}i ],
  [ 'KR', qr{^(?:KR|Korea, Republic of|Republic of Korea|Korea|Südkorea)$}i ],
  [ 'KW', qr{^(?:KW|Kuwait)$}i ],
  [ 'KY', qr{^(?:KY|Cayman Islands)$}i ],
  [ 'KZ', qr{^(?:KZ|Kazakhstan|Kasachstan)$}i ],
  [ 'LA', qr{^(?:LA|Lao People's Democratic Republic|Laos)$}i ],
  [ 'LB', qr{^(?:LB|Lebanon|Libanon)$}i ],
  [ 'LC', qr{^(?:LC|Saint Lucia|St\. Lucia)$}i ],
  [ 'LI', qr{^(?:LI|Liechtenstein)$}i ],
  [ 'LK', qr{^(?:LK|Sri Lanka)$}i ],
  [ 'LR', qr{^(?:LR|Liberia)$}i ],
  [ 'LS', qr{^(?:LS|Lesotho)$}i ],
  [ 'LT', qr{^(?:LT|Lithuania|Litauen)$}i ],
  [ 'LU', qr{^(?:LU|Luxembourg|Luxemburg)$}i ],
  [ 'LV', qr{^(?:LV|Latvia|Lettland)$}i ],
  [ 'LY', qr{^(?:LY|Libya|Libyen)$}i ],
  [ 'MA', qr{^(?:MA|Morocco|Marokko)$}i ],
  [ 'MC', qr{^(?:MC|Monaco)$}i ],
  [ 'MD', qr{^(?:MD|Moldova, Republic of|Moldau)$}i ],
  [ 'ME', qr{^(?:ME|Montenegro)$}i ],
  [ 'MF', qr{^(?:MF|Saint Martin \(French part\)|Saint Martin)$}i ],
  [ 'MG', qr{^(?:MG|Madagascar|Madagaskar)$}i ],
  [ 'MH', qr{^(?:MH|Marshall Islands|Marshallinseln)$}i ],
  [ 'MK', qr{^(?:MK|North Macedonia|Nordmazedonien)$}i ],
  [ 'ML', qr{^(?:ML|Mali)$}i ],
  [ 'MM', qr{^(?:MM|Myanmar)$}i ],
  [ 'MN', qr{^(?:MN|Mongolia|Mongolei)$}i ],
  [ 'MO', qr{^(?:MO|Macao|Macau)$}i ],
  [ 'MP', qr{^(?:MP|Northern Mariana Islands|Nördliche Marianen)$}i ],
  [ 'MQ', qr{^(?:MQ|Martinique)$}i ],
  [ 'MR', qr{^(?:MR|Mauritania|Mauretanien)$}i ],
  [ 'MS', qr{^(?:MS|Montserrat)$}i ],
  [ 'MT', qr{^(?:MT|Malta)$}i ],
  [ 'MU', qr{^(?:MU|Mauritius)$}i ],
  [ 'MV', qr{^(?:MV|Maldives|Malediven)$}i ],
  [ 'MW', qr{^(?:MW|Malawi)$}i ],
  [ 'MX', qr{^(?:MX|Mexico|Mexiko)$}i ],
  [ 'MY', qr{^(?:MY|Malaysia)$}i ],
  [ 'MZ', qr{^(?:MZ|Mozambique|Mosambik)$}i ],
  [ 'NA', qr{^(?:NA|Namibia)$}i ],
  [ 'NC', qr{^(?:NC|New Caledonia|Neukaledonien)$}i ],
  [ 'NE', qr{^(?:NE|Niger)$}i ],
  [ 'NF', qr{^(?:NF|Norfolk Island|Norfolkinsel)$}i ],
  [ 'NG', qr{^(?:NG|Nigeria)$}i ],
  [ 'NI', qr{^(?:NI|Nicaragua)$}i ],
  [ 'NL', qr{^(?:NL|Netherlands|Niederlande)$}i ],
  [ 'NO', qr{^(?:NO|Norway|Norwegen)$}i ],
  [ 'NP', qr{^(?:NP|Nepal)$}i ],
  [ 'NR', qr{^(?:NR|Nauru)$}i ],
  [ 'NU', qr{^(?:NU|Niue)$}i ],
  [ 'NZ', qr{^(?:NZ|New Zealand|Neuseeland)$}i ],
  [ 'OM', qr{^(?:OM|Oman)$}i ],
  [ 'PA', qr{^(?:PA|Panama)$}i ],
  [ 'PE', qr{^(?:PE|Peru)$}i ],
  [ 'PF', qr{^(?:PF|French Polynesia|Französisch-Polynesien)$}i ],
  [ 'PG', qr{^(?:PG|Papua New Guinea|Papua-Neuguinea)$}i ],
  [ 'PH', qr{^(?:PH|Philippines|Philippinen)$}i ],
  [ 'PK', qr{^(?:PK|Pakistan)$}i ],
  [ 'PL', qr{^(?:PL|Poland|Polen)$}i ],
  [ 'PM', qr{^(?:PM|Saint Pierre and Miquelon|Saint-Pierre und Miquelon)$}i ],
  [ 'PN', qr{^(?:PN|Pitcairn|Pitcairninseln)$}i ],
  [ 'PR', qr{^(?:PR|Puerto Rico)$}i ],
  [ 'PS', qr{^(?:PS|Palestine, State of|Palästina)$}i ],
  [ 'PT', qr{^(?:PT|Portugal)$}i ],
  [ 'PW', qr{^(?:PW|Palau)$}i ],
  [ 'PY', qr{^(?:PY|Paraguay)$}i ],
  [ 'QA', qr{^(?:QA|Qatar|Katar)$}i ],
  [ 'RE', qr{^(?:RE|Réunion)$}i ],
  [ 'RO', qr{^(?:RO|Romania|Rumänien)$}i ],
  [ 'RS', qr{^(?:RS|Serbia|Serbien)$}i ],
  [ 'RU', qr{^(?:RU|Russian Federation|Russland)$}i ],
  [ 'RW', qr{^(?:RW|Rwanda|Ruanda)$}i ],
  [ 'SA', qr{^(?:SA|Saudi Arabia|Saudi-Arabien)$}i ],
  [ 'SB', qr{^(?:SB|Solomon Islands|Salomonen)$}i ],
  [ 'SC', qr{^(?:SC|Seychelles)$}i ],
  [ 'SD', qr{^(?:SD|Sudan)$}i ],
  [ 'SE', qr{^(?:SE|Sweden|Schweden)$}i ],
  [ 'SG', qr{^(?:SG|Singapore|Singapur)$}i ],
  [ 'SH', qr{^(?:SH|Saint Helena, Ascension and Tristan da Cunha)$}i ],
  [ 'SI', qr{^(?:SI|Slovenia|Slowenien)$}i ],
  [ 'SJ', qr{^(?:SJ|Svalbard and Jan Mayen|Spitzbergen und Jan Mayen)$}i ],
  [ 'SK', qr{^(?:SK|Slovakia|Slowakei)$}i ],
  [ 'SL', qr{^(?:SL|Sierra Leone)$}i ],
  [ 'SM', qr{^(?:SM|San Marino)$}i ],
  [ 'SN', qr{^(?:SN|Senegal)$}i ],
  [ 'SO', qr{^(?:SO|Somalia)$}i ],
  [ 'SR', qr{^(?:SR|Suriname)$}i ],
  [ 'SS', qr{^(?:SS|South Sudan|Südsudan)$}i ],
  [ 'ST', qr{^(?:ST|Sao Tome and Principe|São Tomé und Príncipe)$}i ],
  [ 'SV', qr{^(?:SV|El Salvador)$}i ],
  [ 'SX', qr{^(?:SX|Sint Maarten \(Dutch part\)|Sint Maarten)$}i ],
  [ 'SY', qr{^(?:SY|Syrian Arab Republic|Syrien)$}i ],
  [ 'SZ', qr{^(?:SZ|Eswatini)$}i ],
  [ 'TC', qr{^(?:TC|Turks and Caicos Islands|Turks- und Caicosinseln)$}i ],
  [ 'TD', qr{^(?:TD|Chad|Tschad)$}i ],
  [ 'TF', qr{^(?:TF|French Southern Territories|Französische Süd- und Antarktisgebiete)$}i ],
  [ 'TG', qr{^(?:TG|Togo)$}i ],
  [ 'TH', qr{^(?:TH|Thailand)$}i ],
  [ 'TJ', qr{^(?:TJ|Tajikistan|Tadschikistan)$}i ],
  [ 'TK', qr{^(?:TK|Tokelau)$}i ],
  [ 'TL', qr{^(?:TL|Timor-Leste|Osttimor)$}i ],
  [ 'TM', qr{^(?:TM|Turkmenistan)$}i ],
  [ 'TN', qr{^(?:TN|Tunisia|Tunesien)$}i ],
  [ 'TO', qr{^(?:TO|Tonga)$}i ],
  [ 'TR', qr{^(?:TR|Turkey|Türkei)$}i ],
  [ 'TT', qr{^(?:TT|Trinidad and Tobago|Trinidad und Tobago)$}i ],
  [ 'TV', qr{^(?:TV|Tuvalu)$}i ],
  [ 'TW', qr{^(?:TW|Taiwan, Province of China|Taiwan)$}i ],
  [ 'TZ', qr{^(?:TZ|Tanzania, United Republic of|Tansania)$}i ],
  [ 'UA', qr{^(?:UA|Ukraine)$}i ],
  [ 'UG', qr{^(?:UG|Uganda)$}i ],
  [ 'UM', qr{^(?:UM|United States Minor Outlying Islands)$}i ],
  [ 'US', qr{^(?:US|United States of America|Vereinigte Staaten)$}i ],
  [ 'UY', qr{^(?:UY|Uruguay)$}i ],
  [ 'UZ', qr{^(?:UZ|Uzbekistan|Usbekistan)$}i ],
  [ 'VA', qr{^(?:VA|Holy See|Vatikanstadt)$}i ],
  [ 'VC', qr{^(?:VC|Saint Vincent and the Grenadines|St\. Vincent und die Grenadinen)$}i ],
  [ 'VE', qr{^(?:VE|Venezuela \(Bolivian Republic of\)|Venezuela)$}i ],
  [ 'VG', qr{^(?:VG|Virgin Islands \(British\)|Britische Jungferninseln)$}i ],
  [ 'VI', qr{^(?:VI|Virgin Islands \(U\.?S\.?\)|Amerikanische Jungferninseln)$}i ],
  [ 'VN', qr{^(?:VN|Viet Nam|Vietnam)$}i ],
  [ 'VU', qr{^(?:VU|Vanuatu)$}i ],
  [ 'WF', qr{^(?:WF|Wallis and Futuna|Wallis und Futuna)$}i ],
  [ 'WS', qr{^(?:WS|Samoa)$}i ],
  [ 'YE', qr{^(?:YE|Yemen|Jemen)$}i ],
  [ 'YT', qr{^(?:YT|Mayotte)$}i ],
  [ 'ZA', qr{^(?:ZA|South Africa|Südafrika)$}i ],
  [ 'ZM', qr{^(?:ZM|Zambia|Sambia)$}i ],
  [ 'ZW', qr{^(?:ZW|Zimbabwe|Simbabwe)$}i ],
);

sub map_name_to_alpha_2_code {
  my ($country) = @_;

  return undef if ($country // '') eq '';

  my $code = first { $country =~ $_->[1] } @alpha_2_mappings;
  return $code->[0] if $code;

  no warnings 'once';
  $::lxdebug->message(LXDebug::WARN(), "ISO3166::map_name_to_alpha_2_code: no mapping found for '$country'");

  return undef;
}

1;
