# Maps RFC 5646 language tags to the first day of the week, with Monday being 0
# and Sunday being 6.
#
# The most common first day of the week is Monday, and has been omitted from
# the list - use it as default value for missing languages.
#
# These were exported from Babel on 2024-06-01:
#
# ```py
# from babel import Locale
# from babel import localedata
# import json
#
# rfc_code = {}
# all_locales = localedata.locale_identifiers()
#
# for locale_code in all_locales:
#     locale = Locale.parse(locale_code)
#
#     if locale.first_week_day == 0:
#         continue
#
#     # Construct the RFC 5646 language tag
#     rfc_locale_code = locale.language
#     if locale.script:
#         rfc_locale_code += "-" + locale.script
#     if locale.territory:
#         rfc_locale_code += "-" + locale.territory
#
#     rfc_locale_code = rfc_locale_code.lower()
#
#     rfc_code[rfc_locale_code] = locale.first_week_day
#
# with open("weekstarts.json", "w") as file:
#     json.dump(rfc_code, file, indent=4)
# ```

_RFC_TO_WEEK_START_DAY = {
    "aa-dj": 5,
    "aa-et": 6,
    "af-za": 6,
    "am-et": 6,
    "apc-sy": 5,
    "ar-ae": 5,
    "ar-bh": 5,
    "ar-dj": 5,
    "ar-dz": 5,
    "ar-eg": 5,
    "ar-il": 6,
    "ar-iq": 5,
    "ar-jo": 5,
    "ar-kw": 5,
    "ar-ly": 5,
    "ar-om": 5,
    "ar-qa": 5,
    "ar-sa": 6,
    "ar-sd": 5,
    "ar-sy": 5,
    "ar-ye": 6,
    "as-in": 6,
    "az-arab-iq": 5,
    "az-arab-ir": 5,
    "bal-arab-pk": 6,
    "bal-latn-pk": 6,
    "bew-id": 6,
    "bgc-in": 6,
    "bgn-ae": 5,
    "bgn-af": 5,
    "bgn-ir": 5,
    "bgn-om": 5,
    "bgn-pk": 6,
    "bho-in": 6,
    "bn-bd": 6,
    "bn-in": 6,
    "bo-in": 6,
    "brx-in": 6,
    "cad-us": 6,
    "ccp-bd": 6,
    "ccp-in": 6,
    "ceb-ph": 6,
    "cho-us": 6,
    "chr-us": 6,
    "cic-us": 6,
    "ckb-iq": 5,
    "ckb-ir": 5,
    "csw-ca": 6,
    "dav-ke": 6,
    "doi-in": 6,
    "dv-mv": 4,
    "dz-bt": 6,
    "ebu-ke": 6,
    "en-ae": 5,
    "en-ag": 6,
    "en-as": 6,
    "en-bs": 6,
    "en-bw": 6,
    "en-bz": 6,
    "en-ca": 6,
    "en-dm": 6,
    "en-dsrt-us": 6,
    "en-gu": 6,
    "en-hk": 6,
    "en-id": 6,
    "en-il": 6,
    "en-in": 6,
    "en-jm": 6,
    "en-ke": 6,
    "en-mh": 6,
    "en-mo": 6,
    "en-mt": 6,
    "en-mv": 4,
    "en-ph": 6,
    "en-pk": 6,
    "en-pr": 6,
    "en-sd": 5,
    "en-sg": 6,
    "en-tt": 6,
    "en-um": 6,
    "en-us": 6,
    "en-vi": 6,
    "en-ws": 6,
    "en-za": 6,
    "en-zw": 6,
    "es-br": 6,
    "es-bz": 6,
    "es-co": 6,
    "es-do": 6,
    "es-gt": 6,
    "es-hn": 6,
    "es-mx": 6,
    "es-ni": 6,
    "es-pa": 6,
    "es-pe": 6,
    "es-ph": 6,
    "es-pr": 6,
    "es-py": 6,
    "es-sv": 6,
    "es-us": 6,
    "es-ve": 6,
    "fa-af": 5,
    "fa-ir": 5,
    "fil-ph": 6,
    "fr-ca": 6,
    "fr-dj": 5,
    "fr-dz": 5,
    "fr-sy": 5,
    "gez-et": 6,
    "gn-py": 6,
    "gu-in": 6,
    "guz-ke": 6,
    "ha-arab-sd": 5,
    "haw-us": 6,
    "he-il": 6,
    "hi-in": 6,
    "hi-latn-in": 6,
    "hnj-hmnp-us": 6,
    "id-id": 6,
    "iu-ca": 6,
    "iu-latn-ca": 6,
    "ja-jp": 6,
    "jv-id": 6,
    "kab-dz": 5,
    "kam-ke": 6,
    "kgp-br": 6,
    "ki-ke": 6,
    "kln-ke": 6,
    "km-kh": 6,
    "kn-in": 6,
    "ko-kr": 6,
    "kok-in": 6,
    "ks-arab-in": 6,
    "ks-deva-in": 6,
    "kxv-deva-in": 6,
    "kxv-latn-in": 6,
    "kxv-orya-in": 6,
    "kxv-telu-in": 6,
    "lkt-us": 6,
    "lo-la": 6,
    "lrc-iq": 5,
    "lrc-ir": 5,
    "luo-ke": 6,
    "luy-ke": 6,
    "mai-in": 6,
    "mas-ke": 6,
    "mer-ke": 6,
    "mgh-mz": 6,
    "mic-ca": 6,
    "ml-in": 6,
    "mni-beng-in": 6,
    "mni-mtei-in": 6,
    "moh-ca": 6,
    "mr-in": 6,
    "ms-id": 6,
    "ms-sg": 6,
    "mt-mt": 6,
    "mus-us": 6,
    "my-mm": 6,
    "mzn-ir": 5,
    "nd-zw": 6,
    "ne-in": 6,
    "ne-np": 6,
    "nr-za": 6,
    "nso-za": 6,
    "nv-us": 6,
    "om-et": 6,
    "om-ke": 6,
    "or-in": 6,
    "osa-us": 6,
    "pa-arab-pk": 6,
    "pa-guru-in": 6,
    "ps-af": 5,
    "ps-pk": 6,
    "pt-ao": 6,
    "pt-br": 6,
    "pt-cv": 6,
    "pt-gq": 6,
    "pt-gw": 6,
    "pt-mo": 6,
    "pt-mz": 6,
    "pt-pt": 6,
    "pt-st": 6,
    "pt-tl": 6,
    "qu-pe": 6,
    "quc-gt": 6,
    "raj-in": 6,
    "rhg-rohg-bd": 6,
    "rhg-rohg-mm": 6,
    "sa-in": 6,
    "saq-ke": 6,
    "sat-deva-in": 6,
    "sat-olck-in": 6,
    "sd-arab-pk": 6,
    "sd-deva-in": 6,
    "sdh-iq": 5,
    "sdh-ir": 5,
    "seh-mz": 6,
    "shn-mm": 6,
    "shn-th": 6,
    "sid-et": 6,
    "skr-pk": 6,
    "sn-zw": 6,
    "so-dj": 5,
    "so-et": 6,
    "so-ke": 6,
    "ss-za": 6,
    "st-za": 6,
    "su-latn-id": 6,
    "sw-ke": 6,
    "syr-iq": 5,
    "syr-sy": 5,
    "ta-in": 6,
    "ta-sg": 6,
    "te-in": 6,
    "teo-ke": 6,
    "th-th": 6,
    "ti-et": 6,
    "tn-bw": 6,
    "tn-za": 6,
    "trv-tw": 6,
    "trw-pk": 6,
    "ts-za": 6,
    "ur-in": 6,
    "ur-pk": 6,
    "uz-arab-af": 5,
    "ve-za": 6,
    "vmw-mz": 6,
    "wal-et": 6,
    "xh-za": 6,
    "xnr-in": 6,
    "yrl-br": 6,
    "yrl-co": 6,
    "yrl-ve": 6,
    "yue-hant-hk": 6,
    "zh-hans-hk": 6,
    "zh-hans-mo": 6,
    "zh-hans-sg": 6,
    "zh-hant-hk": 6,
    "zh-hant-mo": 6,
    "zh-hant-tw": 6,
    "zu-za": 6,
}


def get_week_start_day(language_tag: str) -> int:
    """
    Get the first day of the week for a given language tag.

    This function uses the internal database to find the first day of the week
    for a given language tag. If the language tag is not found, it defaults to
    monday.

    The result is a zero-based index, with Monday being 0 and Sunday being 6.
    """
    # According to stackoverflow some browsers (you just know it's safari) send
    # invalid, lowercase tags. Normalize the tag to avoid issues.
    language_tag = language_tag.lower()

    # Look up the value, defaulting to Monday
    return _RFC_TO_WEEK_START_DAY.get(language_tag, 0)
