"""Interface language tags: normalisation, and what a user's language resolves to."""

import pytest

from navi.locales import SUPPORTED_LOCALES, effective_locale, normalize_locale


@pytest.mark.parametrize("tag,expected", [
    ("en", "en"),
    ("en-US", "en"),
    ("en_US", "en"),
    ("UK", "uk"),
    ("ru-RU", "ru"),
    ("  uk  ", "uk"),
    # A tag's subtags are irrelevant here — only the language prefix matters.
    ("en-GB-oed", "en"),
])
def test_normalize_locale_reads_the_language_prefix(tag, expected):
    assert normalize_locale(tag) == expected


@pytest.mark.parametrize("tag", ["de", "zh-Hans", "", None, "russian", "e"])
def test_normalize_locale_rejects_what_we_do_not_ship(tag):
    assert normalize_locale(tag) is None


def test_every_supported_locale_normalizes_to_itself():
    for code in SUPPORTED_LOCALES:
        assert normalize_locale(code) == code


def test_override_wins_over_the_account():
    assert effective_locale("ru", "uk") == "ru"


def test_account_language_is_used_when_there_is_no_override():
    assert effective_locale(None, "uk") == "uk"
    assert effective_locale("", "ru") == "ru"


def test_english_when_neither_is_known():
    """An unsupported account tag must not leak into the interface — the
    dictionaries only ship en/uk/ru, and English is the fallback everywhere."""
    assert effective_locale(None, None) == "en"
    assert effective_locale("de", "zh") == "en"
