from django.conf import settings
from django.utils.translation import get_language

from oscar_elasticsearch.search.indexing.settings import *  # noqa: F403,F401
from oscar_elasticsearch.search.settings import INDEX_PREFIX


def _lang_suffix() -> str:
    # Catalogue indices in existing projects are commonly generated with `-en`.
    # Allow explicit override, fallback to active language, then `en`.
    lang = (
        getattr(settings, "OSCAR_ELASTICSEARCH_CATALOGUE_LANGUAGE", None)
        or get_language()
        or "en"
    ).lower()
    return lang.split("-")[0]


# Keep storefront/search queries aligned with language-aware indices.
OSCAR_PRODUCTS_INDEX_NAME = f"{INDEX_PREFIX}__catalogue_product-{_lang_suffix()}"
OSCAR_CATEGORIES_INDEX_NAME = f"{INDEX_PREFIX}__catalogue_category-{_lang_suffix()}"
