<?php

namespace Opencart\Extension\Alsendo\Helpers;

class Country
{
    protected static array $map = [
        // European countries
        'AL' => ['albania', 'al', 'shqiperia', 'shqipëria'],
        'AD' => ['andorra', 'ad'],
        'AT' => ['austria', 'at', 'osterreich', 'österreich', 'rakousko'],
        'BY' => ['belarus', 'by', 'bielorussia', 'white russia'],
        'BE' => ['belgium', 'belgia', 'be', 'belgique', 'belgië'],
        'BA' => ['bosnia', 'bosnia and herzegovina', 'bosnia-herzegovina', 'ba', 'bih'],
        'BG' => ['bulgaria', 'bg', 'bulgarien'],
        'HR' => ['croatia', 'hr', 'hrvatska', 'kroatien'],
        'CY' => ['cyprus', 'cy', 'cypern', 'kipros'],
        'CZ' => ['czechia', 'cesko', 'czech', 'cz', 'cehia', 'ceska', 'czech republic'],
        'DK' => ['denmark', 'dk', 'danmark', 'dania'],
        'EE' => ['estonia', 'ee', 'eesti', 'estland'],
        'FI' => ['finland', 'fi', 'suomi', 'finnland'],
        'FR' => ['france', 'francja', 'franta', 'fr', 'francia'],
        'DE' => ['germany', 'deutschland', 'niemcy', 'germania', 'de'],
        'GR' => ['greece', 'gr', 'hellas', 'ellada', 'grecja'],
        'HU' => ['hungary', 'wegry', 'ungaria', 'hu', 'hungaria', 'magyarorszag'],
        'IS' => ['iceland', 'is', 'island'],
        'IE' => ['ireland', 'ie', 'eire', 'irland'],
        'IT' => ['italy', 'wlochy', 'italia', 'it', 'italia'],
        'XK' => ['kosovo', 'xk', 'kosovo, republic of'],
        'LV' => ['latvia', 'lv', 'latvija', 'lettland'],
        'LI' => ['liechtenstein', 'li'],
        'LT' => ['lithuania', 'lt', 'lietuva', 'litauen'],
        'LU' => ['luxembourg', 'lu', 'luxemburg', 'letzebuerg'],
        'MT' => ['malta', 'mt'],
        'MD' => ['moldova', 'md', 'moldavia', 'moldova, republic of'],
        'MC' => ['monaco', 'mc'],
        'ME' => ['montenegro', 'me', 'crna gora'],
        'NL' => ['netherlands', 'holandia', 'nl', 'olanda', 'holland', 'nederland'],
        'MK' => ['north macedonia', 'macedonia', 'mk', 'makedonija', 'fyrom'],
        'NO' => ['norway', 'no', 'norge', 'norwegen'],
        'PL' => ['poland', 'polska', 'polsko', 'polonia', 'pl'],
        'PT' => ['portugal', 'portugalia', 'pt', 'portugalia'],
        'RO' => ['romania', 'romania', 'românia', 'ro', 'romania'],
        'RU' => ['russia', 'ru', 'russian federation', 'rossiya'],
        'SM' => ['san marino', 'sm'],
        'RS' => ['serbia', 'rs', 'srbija', 'serbien'],
        'SK' => ['slovakia', 'slowacja', 'slovensko', 'sk', 'slovacia', 'slovak republic'],
        'SI' => ['slovenia', 'si', 'slovenija', 'slowenien'],
        'ES' => ['spain', 'hiszpania', 'espana', 'es', 'spania', 'espanya'],
        'SE' => ['sweden', 'se', 'sverige', 'schweden'],
        'CH' => ['switzerland', 'ch', 'schweiz', 'suisse', 'svizzera', 'szwajcaria'],
        'TR' => ['turkey', 'tr', 'turkiye', 'türkiye', 'turquia'],
        'UA' => ['ukraine', 'ua', 'ukraina', 'ukrajna'],
        'GB' => ['united kingdom', 'uk', 'gb', 'great britain', 'britain', 'england', 'britannia'],
        'VA' => ['vatican', 'vatican city', 'va', 'holy see'],
        // Non-European (common / major)
        'US' => ['usa', 'united states', 'us', 'u.s.a.', 'united states of america', 'america'],
        'MX' => ['mexico', 'mx', 'mexiko', 'méxico'],
        'CN' => ['china', 'cn', 'prc', "people's republic of china", 'chiny'],
        'BR' => ['brazil', 'br', 'brasil', 'brazilia'],
        'IN' => ['india', 'in', 'bharat', 'indie'],
        'AU' => ['australia', 'au', 'australie'],
        'CA' => ['canada', 'ca', 'kanada'],
        'JP' => ['japan', 'jp', 'japon', 'nippon'],
        'KR' => ['south korea', 'korea', 'kr', 'korea republic', 'republic of korea'],
        'AR' => ['argentina', 'ar', 'argentine'],
        'CL' => ['chile', 'cl', 'chile'],
        'CO' => ['colombia', 'co', 'kolumbien'],
        'ZA' => ['south africa', 'za', 'rsa', 'republic of south africa'],
        'EG' => ['egypt', 'eg', 'egypte', 'egipt'],
        'NG' => ['nigeria', 'ng'],
        'SA' => ['saudi arabia', 'sa', 'ksa', 'saudi'],
        'AE' => ['uae', 'united arab emirates', 'ae', 'emirates'],
        'IL' => ['israel', 'il'],
        'TH' => ['thailand', 'th', 'siam'],
        'VN' => ['vietnam', 'vn', 'viet nam', 'vietnam'],
        'ID' => ['indonesia', 'id'],
        'MY' => ['malaysia', 'my'],
        'PH' => ['philippines', 'ph', 'philippine'],
        'NZ' => ['new zealand', 'nz', 'nz'],
        'PK' => ['pakistan', 'pk'],
        'BD' => ['bangladesh', 'bd'],
    ];

    protected static array $translatableMap = [
        'AL' => 'text_country_name_albania',
        'AD' => 'text_country_name_andorra',
        'AT' => 'text_country_name_austria',
        'BY' => 'text_country_name_belarus',
        'BE' => 'text_country_name_belgium',
        'BA' => 'text_country_name_bosnia_herzegovina',
        'BG' => 'text_country_name_bulgaria',
        'HR' => 'text_country_name_croatia',
        'CY' => 'text_country_name_cyprus',
        'CZ' => 'text_country_name_czechia',
        'DK' => 'text_country_name_denmark',
        'EE' => 'text_country_name_estonia',
        'FI' => 'text_country_name_finland',
        'FR' => 'text_country_name_france',
        'DE' => 'text_country_name_germany',
        'GR' => 'text_country_name_greece',
        'HU' => 'text_country_name_hungary',
        'IS' => 'text_country_name_iceland',
        'IE' => 'text_country_name_ireland',
        'IT' => 'text_country_name_italy',
        'XK' => 'text_country_name_kosovo',
        'LV' => 'text_country_name_latvia',
        'LI' => 'text_country_name_liechtenstein',
        'LT' => 'text_country_name_lithuania',
        'LU' => 'text_country_name_luxembourg',
        'MT' => 'text_country_name_malta',
        'MD' => 'text_country_name_moldova',
        'MC' => 'text_country_name_monaco',
        'ME' => 'text_country_name_montenegro',
        'NL' => 'text_country_name_netherlands',
        'MK' => 'text_country_name_north_macedonia',
        'NO' => 'text_country_name_norway',
        'PL' => 'text_country_name_poland',
        'PT' => 'text_country_name_portugal',
        'RO' => 'text_country_name_romania',
        'RU' => 'text_country_name_russia',
        'SM' => 'text_country_name_san_marino',
        'RS' => 'text_country_name_serbia',
        'SK' => 'text_country_name_slovakia',
        'SI' => 'text_country_name_slovenia',
        'ES' => 'text_country_name_spain',
        'SE' => 'text_country_name_sweden',
        'CH' => 'text_country_name_switzerland',
        'TR' => 'text_country_name_turkey',
        'UA' => 'text_country_name_ukraine',
        'GB' => 'text_country_name_united_kingdom',
        'VA' => 'text_country_name_vatican_city'
    ];

    public static function getIsoCode(string $name): ?string
    {
        $name = mb_strtolower($name);
        foreach (self::$map as $iso => $names) {
            if (in_array($name, $names, true)) {
                return $iso;
            }
        }
        return null;
    }

    public static function getTranslatableMap(): array
    {
        return self::$translatableMap;
    }
}