"""Search indexes utils."""
from django_elasticsearch_dsl import fields
from utils.models import get_current_locale, get_default_locale

FACET_MAX_RESPONSE = 9999999 # Unlimited

ALL_LOCALES_LIST = [
    'hr-HR',
    'ro-RO',
    'sl-SI',
    'ka-GE',
    'de-AT',
    'de-DE',
    'el-GR',
    'hu-HU',
    'nl-BE',
    'ja-JP',
    'it-IT',
    'pl-PL',
    'he-IL',
    'pt-BR',
    'hu_HU',
]

# object field properties
OBJECT_FIELD_PROPERTIES = {locale: fields.TextField() for locale in ALL_LOCALES_LIST}
OBJECT_FIELD_PROPERTIES.update({
    'en-AU': fields.TextField(analyzer='english'),
    'en-US': fields.TextField(analyzer='english'),
    'en-GB': fields.TextField(analyzer='english'),
    'en-CA': fields.TextField(analyzer='english'),
    'ru-RU': fields.TextField(analyzer='russian'),
    'fr-FR': fields.TextField(analyzer='french'),
    'fr-BE': fields.TextField(analyzer='french'),
    'fr-MA': fields.TextField(analyzer='french'),
    'fr-CA': fields.TextField(analyzer='french'),
})


# todo: refactor serializer
def get_translated_value(value):
    if value is None:
        return None
    elif not isinstance(value, dict):
        field_dict = value.to_dict()
    else:
        field_dict = value
    field_dict = {k: v for k, v in field_dict.items() if v is not None}
    result = field_dict.get(get_current_locale(), None)
    # fallback
    if result is None:
        result = field_dict.get(get_default_locale(), None)
        if result is None:
            values = list(field_dict.values())
            result = values[0] if values else None
    return result
