"""gettext bootstrap. Import `_`/`ngettext` for runtime translation; `N_` marks
deferred strings (module-level constants) so xgettext extracts them while leaving
the value untranslated until used at the call site with `_()`.
"""

from __future__ import annotations

import gettext
import os
from collections.abc import Callable
from pathlib import Path

DOMAIN = "big-ddcgui"


def _localedir() -> str:
    """Resolve the gettext locale dir across system, Nix and AppImage installs.

    The wheel lands in <prefix>/lib/pythonX.Y/site-packages/big_ddcgui while the
    catalogs live in <prefix>/share/locale. On a system install <prefix> is /usr;
    on Nix it is the immutable store path; under AppImage it is the AppDir root.
    An explicit BIG_DDCGUI_LOCALEDIR override wins (tests / relocated installs).
    """
    override = os.environ.get("BIG_DDCGUI_LOCALEDIR")
    if override and Path(override).is_dir():
        return override
    for parent in Path(__file__).resolve().parents:
        for rel in ("share/locale", "usr/share/locale"):
            candidate = parent / rel
            if candidate.is_dir():
                return str(candidate)
    return "/usr/share/locale"


_translation = gettext.translation(DOMAIN, localedir=_localedir(), fallback=True)

_: Callable[[str], str] = _translation.gettext
ngettext: Callable[[str, str, int], str] = _translation.ngettext


def N_(message: str) -> str:
    """No-op marker for xgettext extraction of deferred strings."""
    return message
