import xml.etree.ElementTree as ET
from pathlib import Path
from typing import MutableMapping

__all__ = [
    "get_infos",
    "get_extended_infos",
    "version_cldr",
    "version_emoji",
]

version_cldr: str = "45"  # Unicode CLDR from 2024-04-17. See https://cldr.unicode.org/index/downloads
version_emoji: str = "15.1"  # Unicode Emoji from 2023-09-05. See https://www.unicode.org/reports/tr51/

_FlagInfo = MutableMapping[str, str | bool | None]
_FlagInfos = MutableMapping[str, _FlagInfo]

_hardcoded: _FlagInfos = {
    "EU": {"id_status": "macroregion", "supported": True, "valid": True},
    "UN": {"id_status": "macroregion", "supported": True, "valid": True},
    "AC": {"id_status": "regular", "supported": True, "valid": True},
    "AD": {"id_status": "regular", "supported": True, "valid": True},
    "AE": {"id_status": "regular", "supported": True, "valid": True},
    "AF": {"id_status": "regular", "supported": True, "valid": True},
    "AG": {"id_status": "regular", "supported": True, "valid": True},
    "AI": {"id_status": "regular", "supported": True, "valid": True},
    "AL": {"id_status": "regular", "supported": True, "valid": True},
    "AM": {"id_status": "regular", "supported": True, "valid": True},
    "AO": {"id_status": "regular", "supported": True, "valid": True},
    "AQ": {"id_status": "regular", "supported": True, "valid": True},
    "AR": {"id_status": "regular", "supported": True, "valid": True},
    "AS": {"id_status": "regular", "supported": True, "valid": True},
    "AT": {"id_status": "regular", "supported": True, "valid": True},
    "AU": {"id_status": "regular", "supported": True, "valid": True},
    "AW": {"id_status": "regular", "supported": True, "valid": True},
    "AX": {"id_status": "regular", "supported": True, "valid": True},
    "AZ": {"id_status": "regular", "supported": True, "valid": True},
    "BA": {"id_status": "regular", "supported": True, "valid": True},
    "BB": {"id_status": "regular", "supported": True, "valid": True},
    "BD": {"id_status": "regular", "supported": True, "valid": True},
    "BE": {"id_status": "regular", "supported": True, "valid": True},
    "BF": {"id_status": "regular", "supported": True, "valid": True},
    "BG": {"id_status": "regular", "supported": True, "valid": True},
    "BH": {"id_status": "regular", "supported": True, "valid": True},
    "BI": {"id_status": "regular", "supported": True, "valid": True},
    "BJ": {"id_status": "regular", "supported": True, "valid": True},
    "BL": {"id_status": "regular", "supported": True, "valid": True},
    "BM": {"id_status": "regular", "supported": True, "valid": True},
    "BN": {"id_status": "regular", "supported": True, "valid": True},
    "BO": {"id_status": "regular", "supported": True, "valid": True},
    "BQ": {"id_status": "regular", "supported": True, "valid": True},
    "BR": {"id_status": "regular", "supported": True, "valid": True},
    "BS": {"id_status": "regular", "supported": True, "valid": True},
    "BT": {"id_status": "regular", "supported": True, "valid": True},
    "BV": {"id_status": "regular", "supported": True, "valid": True},
    "BW": {"id_status": "regular", "supported": True, "valid": True},
    "BY": {"id_status": "regular", "supported": True, "valid": True},
    "BZ": {"id_status": "regular", "supported": True, "valid": True},
    "CA": {"id_status": "regular", "supported": True, "valid": True},
    "CC": {"id_status": "regular", "supported": True, "valid": True},
    "CD": {"id_status": "regular", "supported": True, "valid": True},
    "CF": {"id_status": "regular", "supported": True, "valid": True},
    "CG": {"id_status": "regular", "supported": True, "valid": True},
    "CH": {"id_status": "regular", "supported": True, "valid": True},
    "CI": {"id_status": "regular", "supported": True, "valid": True},
    "CK": {"id_status": "regular", "supported": True, "valid": True},
    "CL": {"id_status": "regular", "supported": True, "valid": True},
    "CM": {"id_status": "regular", "supported": True, "valid": True},
    "CN": {"id_status": "regular", "supported": True, "valid": True},
    "CO": {"id_status": "regular", "supported": True, "valid": True},
    "CP": {"id_status": "regular", "supported": True, "valid": True},
    "CQ": {"id_status": "regular", "supported": True, "valid": True},
    "CR": {"id_status": "regular", "supported": True, "valid": True},
    "CU": {"id_status": "regular", "supported": True, "valid": True},
    "CV": {"id_status": "regular", "supported": True, "valid": True},
    "CW": {"id_status": "regular", "supported": True, "valid": True},
    "CX": {"id_status": "regular", "supported": True, "valid": True},
    "CY": {"id_status": "regular", "supported": True, "valid": True},
    "CZ": {"id_status": "regular", "supported": True, "valid": True},
    "DE": {"id_status": "regular", "supported": True, "valid": True},
    "DG": {"id_status": "regular", "supported": True, "valid": True},
    "DJ": {"id_status": "regular", "supported": True, "valid": True},
    "DK": {"id_status": "regular", "supported": True, "valid": True},
    "DM": {"id_status": "regular", "supported": True, "valid": True},
    "DO": {"id_status": "regular", "supported": True, "valid": True},
    "DZ": {"id_status": "regular", "supported": True, "valid": True},
    "EA": {"id_status": "regular", "supported": True, "valid": True},
    "EC": {"id_status": "regular", "supported": True, "valid": True},
    "EE": {"id_status": "regular", "supported": True, "valid": True},
    "EG": {"id_status": "regular", "supported": True, "valid": True},
    "EH": {"id_status": "regular", "supported": True, "valid": True},
    "ER": {"id_status": "regular", "supported": True, "valid": True},
    "ES": {"id_status": "regular", "supported": True, "valid": True},
    "ET": {"id_status": "regular", "supported": True, "valid": True},
    "FI": {"id_status": "regular", "supported": True, "valid": True},
    "FJ": {"id_status": "regular", "supported": True, "valid": True},
    "FK": {"id_status": "regular", "supported": True, "valid": True},
    "FM": {"id_status": "regular", "supported": True, "valid": True},
    "FO": {"id_status": "regular", "supported": True, "valid": True},
    "FR": {"id_status": "regular", "supported": True, "valid": True},
    "GA": {"id_status": "regular", "supported": True, "valid": True},
    "GB": {"id_status": "regular", "supported": True, "valid": True},
    "GD": {"id_status": "regular", "supported": True, "valid": True},
    "GE": {"id_status": "regular", "supported": True, "valid": True},
    "GF": {"id_status": "regular", "supported": True, "valid": True},
    "GG": {"id_status": "regular", "supported": True, "valid": True},
    "GH": {"id_status": "regular", "supported": True, "valid": True},
    "GI": {"id_status": "regular", "supported": True, "valid": True},
    "GL": {"id_status": "regular", "supported": True, "valid": True},
    "GM": {"id_status": "regular", "supported": True, "valid": True},
    "GN": {"id_status": "regular", "supported": True, "valid": True},
    "GP": {"id_status": "regular", "supported": True, "valid": True},
    "GQ": {"id_status": "regular", "supported": True, "valid": True},
    "GR": {"id_status": "regular", "supported": True, "valid": True},
    "GS": {"id_status": "regular", "supported": True, "valid": True},
    "GT": {"id_status": "regular", "supported": True, "valid": True},
    "GU": {"id_status": "regular", "supported": True, "valid": True},
    "GW": {"id_status": "regular", "supported": True, "valid": True},
    "GY": {"id_status": "regular", "supported": True, "valid": True},
    "HK": {"id_status": "regular", "supported": True, "valid": True},
    "HM": {"id_status": "regular", "supported": True, "valid": True},
    "HN": {"id_status": "regular", "supported": True, "valid": True},
    "HR": {"id_status": "regular", "supported": True, "valid": True},
    "HT": {"id_status": "regular", "supported": True, "valid": True},
    "HU": {"id_status": "regular", "supported": True, "valid": True},
    "IC": {"id_status": "regular", "supported": True, "valid": True},
    "ID": {"id_status": "regular", "supported": True, "valid": True},
    "IE": {"id_status": "regular", "supported": True, "valid": True},
    "IL": {"id_status": "regular", "supported": True, "valid": True},
    "IM": {"id_status": "regular", "supported": True, "valid": True},
    "IN": {"id_status": "regular", "supported": True, "valid": True},
    "IO": {"id_status": "regular", "supported": True, "valid": True},
    "IQ": {"id_status": "regular", "supported": True, "valid": True},
    "IR": {"id_status": "regular", "supported": True, "valid": True},
    "IS": {"id_status": "regular", "supported": True, "valid": True},
    "IT": {"id_status": "regular", "supported": True, "valid": True},
    "JE": {"id_status": "regular", "supported": True, "valid": True},
    "JM": {"id_status": "regular", "supported": True, "valid": True},
    "JO": {"id_status": "regular", "supported": True, "valid": True},
    "JP": {"id_status": "regular", "supported": True, "valid": True},
    "KE": {"id_status": "regular", "supported": True, "valid": True},
    "KG": {"id_status": "regular", "supported": True, "valid": True},
    "KH": {"id_status": "regular", "supported": True, "valid": True},
    "KI": {"id_status": "regular", "supported": True, "valid": True},
    "KM": {"id_status": "regular", "supported": True, "valid": True},
    "KN": {"id_status": "regular", "supported": True, "valid": True},
    "KP": {"id_status": "regular", "supported": True, "valid": True},
    "KR": {"id_status": "regular", "supported": True, "valid": True},
    "KW": {"id_status": "regular", "supported": True, "valid": True},
    "KY": {"id_status": "regular", "supported": True, "valid": True},
    "KZ": {"id_status": "regular", "supported": True, "valid": True},
    "LA": {"id_status": "regular", "supported": True, "valid": True},
    "LB": {"id_status": "regular", "supported": True, "valid": True},
    "LC": {"id_status": "regular", "supported": True, "valid": True},
    "LI": {"id_status": "regular", "supported": True, "valid": True},
    "LK": {"id_status": "regular", "supported": True, "valid": True},
    "LR": {"id_status": "regular", "supported": True, "valid": True},
    "LS": {"id_status": "regular", "supported": True, "valid": True},
    "LT": {"id_status": "regular", "supported": True, "valid": True},
    "LU": {"id_status": "regular", "supported": True, "valid": True},
    "LV": {"id_status": "regular", "supported": True, "valid": True},
    "LY": {"id_status": "regular", "supported": True, "valid": True},
    "MA": {"id_status": "regular", "supported": True, "valid": True},
    "MC": {"id_status": "regular", "supported": True, "valid": True},
    "MD": {"id_status": "regular", "supported": True, "valid": True},
    "ME": {"id_status": "regular", "supported": True, "valid": True},
    "MF": {"id_status": "regular", "supported": True, "valid": True},
    "MG": {"id_status": "regular", "supported": True, "valid": True},
    "MH": {"id_status": "regular", "supported": True, "valid": True},
    "MK": {"id_status": "regular", "supported": True, "valid": True},
    "ML": {"id_status": "regular", "supported": True, "valid": True},
    "MM": {"id_status": "regular", "supported": True, "valid": True},
    "MN": {"id_status": "regular", "supported": True, "valid": True},
    "MO": {"id_status": "regular", "supported": True, "valid": True},
    "MP": {"id_status": "regular", "supported": True, "valid": True},
    "MQ": {"id_status": "regular", "supported": True, "valid": True},
    "MR": {"id_status": "regular", "supported": True, "valid": True},
    "MS": {"id_status": "regular", "supported": True, "valid": True},
    "MT": {"id_status": "regular", "supported": True, "valid": True},
    "MU": {"id_status": "regular", "supported": True, "valid": True},
    "MV": {"id_status": "regular", "supported": True, "valid": True},
    "MW": {"id_status": "regular", "supported": True, "valid": True},
    "MX": {"id_status": "regular", "supported": True, "valid": True},
    "MY": {"id_status": "regular", "supported": True, "valid": True},
    "MZ": {"id_status": "regular", "supported": True, "valid": True},
    "NA": {"id_status": "regular", "supported": True, "valid": True},
    "NC": {"id_status": "regular", "supported": True, "valid": True},
    "NE": {"id_status": "regular", "supported": True, "valid": True},
    "NF": {"id_status": "regular", "supported": True, "valid": True},
    "NG": {"id_status": "regular", "supported": True, "valid": True},
    "NI": {"id_status": "regular", "supported": True, "valid": True},
    "NL": {"id_status": "regular", "supported": True, "valid": True},
    "NO": {"id_status": "regular", "supported": True, "valid": True},
    "NP": {"id_status": "regular", "supported": True, "valid": True},
    "NR": {"id_status": "regular", "supported": True, "valid": True},
    "NU": {"id_status": "regular", "supported": True, "valid": True},
    "NZ": {"id_status": "regular", "supported": True, "valid": True},
    "OM": {"id_status": "regular", "supported": True, "valid": True},
    "PA": {"id_status": "regular", "supported": True, "valid": True},
    "PE": {"id_status": "regular", "supported": True, "valid": True},
    "PF": {"id_status": "regular", "supported": True, "valid": True},
    "PG": {"id_status": "regular", "supported": True, "valid": True},
    "PH": {"id_status": "regular", "supported": True, "valid": True},
    "PK": {"id_status": "regular", "supported": True, "valid": True},
    "PL": {"id_status": "regular", "supported": True, "valid": True},
    "PM": {"id_status": "regular", "supported": True, "valid": True},
    "PN": {"id_status": "regular", "supported": True, "valid": True},
    "PR": {"id_status": "regular", "supported": True, "valid": True},
    "PS": {"id_status": "regular", "supported": True, "valid": True},
    "PT": {"id_status": "regular", "supported": True, "valid": True},
    "PW": {"id_status": "regular", "supported": True, "valid": True},
    "PY": {"id_status": "regular", "supported": True, "valid": True},
    "QA": {"id_status": "regular", "supported": True, "valid": True},
    "RE": {"id_status": "regular", "supported": True, "valid": True},
    "RO": {"id_status": "regular", "supported": True, "valid": True},
    "RS": {"id_status": "regular", "supported": True, "valid": True},
    "RU": {"id_status": "regular", "supported": True, "valid": True},
    "RW": {"id_status": "regular", "supported": True, "valid": True},
    "SA": {"id_status": "regular", "supported": True, "valid": True},
    "SB": {"id_status": "regular", "supported": True, "valid": True},
    "SC": {"id_status": "regular", "supported": True, "valid": True},
    "SD": {"id_status": "regular", "supported": True, "valid": True},
    "SE": {"id_status": "regular", "supported": True, "valid": True},
    "SG": {"id_status": "regular", "supported": True, "valid": True},
    "SH": {"id_status": "regular", "supported": True, "valid": True},
    "SI": {"id_status": "regular", "supported": True, "valid": True},
    "SJ": {"id_status": "regular", "supported": True, "valid": True},
    "SK": {"id_status": "regular", "supported": True, "valid": True},
    "SL": {"id_status": "regular", "supported": True, "valid": True},
    "SM": {"id_status": "regular", "supported": True, "valid": True},
    "SN": {"id_status": "regular", "supported": True, "valid": True},
    "SO": {"id_status": "regular", "supported": True, "valid": True},
    "SR": {"id_status": "regular", "supported": True, "valid": True},
    "SS": {"id_status": "regular", "supported": True, "valid": True},
    "ST": {"id_status": "regular", "supported": True, "valid": True},
    "SV": {"id_status": "regular", "supported": True, "valid": True},
    "SX": {"id_status": "regular", "supported": True, "valid": True},
    "SY": {"id_status": "regular", "supported": True, "valid": True},
    "SZ": {"id_status": "regular", "supported": True, "valid": True},
    "TA": {"id_status": "regular", "supported": True, "valid": True},
    "TC": {"id_status": "regular", "supported": True, "valid": True},
    "TD": {"id_status": "regular", "supported": True, "valid": True},
    "TF": {"id_status": "regular", "supported": True, "valid": True},
    "TG": {"id_status": "regular", "supported": True, "valid": True},
    "TH": {"id_status": "regular", "supported": True, "valid": True},
    "TJ": {"id_status": "regular", "supported": True, "valid": True},
    "TK": {"id_status": "regular", "supported": True, "valid": True},
    "TL": {"id_status": "regular", "supported": True, "valid": True},
    "TM": {"id_status": "regular", "supported": True, "valid": True},
    "TN": {"id_status": "regular", "supported": True, "valid": True},
    "TO": {"id_status": "regular", "supported": True, "valid": True},
    "TR": {"id_status": "regular", "supported": True, "valid": True},
    "TT": {"id_status": "regular", "supported": True, "valid": True},
    "TV": {"id_status": "regular", "supported": True, "valid": True},
    "TW": {"id_status": "regular", "supported": True, "valid": True},
    "TZ": {"id_status": "regular", "supported": True, "valid": True},
    "UA": {"id_status": "regular", "supported": True, "valid": True},
    "UG": {"id_status": "regular", "supported": True, "valid": True},
    "UM": {"id_status": "regular", "supported": True, "valid": True},
    "US": {"id_status": "regular", "supported": True, "valid": True},
    "UY": {"id_status": "regular", "supported": True, "valid": True},
    "UZ": {"id_status": "regular", "supported": True, "valid": True},
    "VA": {"id_status": "regular", "supported": True, "valid": True},
    "VC": {"id_status": "regular", "supported": True, "valid": True},
    "VE": {"id_status": "regular", "supported": True, "valid": True},
    "VG": {"id_status": "regular", "supported": True, "valid": True},
    "VI": {"id_status": "regular", "supported": True, "valid": True},
    "VN": {"id_status": "regular", "supported": True, "valid": True},
    "VU": {"id_status": "regular", "supported": True, "valid": True},
    "WF": {"id_status": "regular", "supported": True, "valid": True},
    "WS": {"id_status": "regular", "supported": True, "valid": True},
    "XK": {"id_status": "regular", "supported": True, "valid": True},
    "YE": {"id_status": "regular", "supported": True, "valid": True},
    "YT": {"id_status": "regular", "supported": True, "valid": True},
    "ZA": {"id_status": "regular", "supported": True, "valid": True},
    "ZM": {"id_status": "regular", "supported": True, "valid": True},
    "ZW": {"id_status": "regular", "supported": True, "valid": True},
    "gbeng": {"id_status": "regular", "supported": True, "valid": True},
    "gbsct": {"id_status": "regular", "supported": True, "valid": True},
    "gbwls": {"id_status": "regular", "supported": True, "valid": True},
    "UK": {
        "id_status": None,
        "supported": False,
        "valid": False,
        "comment": "United Kingdom, the canonicalized form is GB",
    },
    "AN": {"id_status": "deprecated", "supported": False, "valid": True},
    "BU": {"id_status": "deprecated", "supported": False, "valid": True},
    "CS": {"id_status": "deprecated", "supported": False, "valid": True},
    "DD": {"id_status": "deprecated", "supported": False, "valid": True},
    "FX": {"id_status": "deprecated", "supported": False, "valid": True},
    "NT": {"id_status": "deprecated", "supported": False, "valid": True},
    "QU": {
        "id_status": "deprecated",
        "supported": False,
        "valid": True,
        "comment": "European Union, the canonicalized form is EU",
    },
    "SU": {"id_status": "deprecated", "supported": False, "valid": True},
    "TP": {"id_status": "deprecated", "supported": False, "valid": True},
    "YD": {"id_status": "deprecated", "supported": False, "valid": True},
    "YU": {"id_status": "deprecated", "supported": False, "valid": True},
    "ZR": {"id_status": "deprecated", "supported": False, "valid": True},
    "XT": {
        "id_status": "private_use",
        "supported": False,
        "valid": False,
        "comment": "Texas, supported in WhatsApp",
    },
    "ustx": {
        "id_status": "regular",
        "supported": False,
        "valid": False,
        "comment": "Texas, supported in WhatsApp",
    },
}

_flag_data = _hardcoded
_flag_data_is_extended = False


def get_infos() -> _FlagInfos:
    """Returns the hardcoded flag or if get_extended_infos() was called before, the extended flag data."""
    return _flag_data


def get_extended_infos() -> _FlagInfos:
    """Return the extended flag data from Unicode CLDR region.xml and subdivision.xml"""
    global _flag_data_is_extended

    if _flag_data_is_extended:
        return _flag_data

    this_file = Path(__file__)
    _supplemental_data(this_file.with_name("region.xml"))
    _supplemental_data(this_file.with_name("subdivision.xml"))

    _flag_data_is_extended = True

    return _flag_data


def _supplemental_data(xml_file_path: Path):
    """Load supplemental data from CLDR XML files into _flag_data"""
    def setter(seq: str, id_status: str):
        if seq not in _flag_data:
            _flag_data[seq] = {
                "id_status": id_status,
                "supported": False,
                "valid": False,
            }
        elif "id_status" not in _flag_data[seq]:
            _flag_data[seq]["id_status"] = id_status

    tree = ET.parse(xml_file_path)
    root = tree.getroot()

    id_validity = root.find("idValidity")
    assert id_validity is not None
    for id_group in id_validity:
        id_status = id_group.attrib["idStatus"]
        assert id_group.text is not None
        for specifier in id_group.text.split():
            if "~" in specifier:
                seq, end = specifier.split("~")
                while seq[-1] <= end and seq[-1] <= "z":
                    setter(seq, id_status)
                    seq = seq[0:-1] + chr(ord(seq[-1]) + 1)
            else:
                setter(specifier, id_status)
