"""src/eones/locales/__init__.py"""

from __future__ import annotations

from functools import lru_cache
from importlib import import_module
from typing import Any, Dict, Tuple, Union, cast

DEFAULT_LOCALE = "en"


@lru_cache(maxsize=None)
def get_messages(locale: str) -> Dict[str, Union[str, Tuple[str, str]]]:
    """Load locale messages dynamically from ``eones.locales`` modules.

    Falls back to English if the given locale is not available.
    """
    try:
        module = import_module(f"eones.locales.{locale}")

    except ModuleNotFoundError:
        module = import_module(f"eones.locales.{DEFAULT_LOCALE}")

    return cast(Dict[str, Union[str, Tuple[str, str]]], getattr(module, "MESSAGES"))
