"""
Mapping of ISO 639-3 language codes used by Scribe to ISO 639-1
used by Livekit turn detector multilingual model
Note: ISO 639-3 language codes supported by Scribe but that have no ISO 639-1 code
are defined with a None mapping value
https://elevenlabs.io/docs/overview/capabilities/speech-to-text#supported-languages
"""

ISO_639_3_TO_1 = {
    "afr": "af",
    "amh": "am",
    "ara": "ar",
    "hye": "hy",
    "asm": "as",
    "ast": None,
    "aze": "az",
    "bel": "be",
    "ben": "bn",
    "bos": "bs",
    "bul": "bg",
    "mya": "my",
    "yue": None,
    "cat": "ca",
    "ceb": None,
    "nya": "ny",
    "hrv": "hr",
    "ces": "cs",
    "dan": "da",
    "nld": "nl",
    "eng": "en",
    "est": "et",
    "fil": None,
    "fin": "fi",
    "fra": "fr",
    "ful": "ff",
    "glg": "gl",
    "lug": "lg",
    "kat": "ka",
    "deu": "de",
    "ell": "el",
    "guj": "gu",
    "hau": "ha",
    "heb": "he",
    "hin": "hi",
    "hun": "hu",
    "isl": "is",
    "ibo": "ig",
    "ind": "id",
    "gle": "ga",
    "ita": "it",
    "jpn": "ja",
    "jav": "jv",
    "kea": None,
    "kan": "kn",
    "kaz": "kk",
    "khm": "km",
    "kor": "ko",
    "kur": "ku",
    "kir": "ky",
    "lao": "lo",
    "lav": "lv",
    "lin": "ln",
    "lit": "lt",
    "luo": None,
    "ltz": "lb",
    "mkd": "mk",
    "msa": "ms",
    "mal": "ml",
    "mlt": "mt",
    "zho": "zh",
    "mri": "mi",
    "mar": "mr",
    "mon": "mn",
    "nep": "ne",
    "nso": None,
    "nor": "no",
    "oci": "oc",
    "ori": "or",
    "pus": "ps",
    "fas": "fa",
    "pol": "pl",
    "por": "pt",
    "pan": "pa",
    "ron": "ro",
    "rus": "ru",
    "srp": "sr",
    "sna": "sn",
    "snd": "sd",
    "slk": "sk",
    "slv": "sl",
    "som": "so",
    "spa": "es",
    "swa": "sw",
    "swe": "sv",
    "tam": "ta",
    "tgk": "tg",
    "tel": "te",
    "tha": "th",
    "tur": "tr",
    "ukr": "uk",
    "umb": None,
    "urd": "ur",
    "uzb": "uz",
    "vie": "vi",
    "cym": "cy",
    "wol": "wo",
    "xho": "xh",
    "zul": "zu",
}


def iso639_3_to_1(code: str | None) -> str | None:
    """Convert ISO 639-3 to ISO 639-1. Returns None if no mapping exists."""
    return ISO_639_3_TO_1.get(code.lower()) if code else None
