# Path: modeltrans/conf.py
# def get_default_language():
#     return settings.LANGUAGE_CODE
# 
# def get_fallback_chain(lang):
#     """
#     Returns the list of fallback languages for language `lang`.
# 
#     For example, this function will return `('nl', 'en')` when called
#     with `lang='fy'` and configured like this::
# 
#         LANGUAGE_CODE = 'en'
# 
#         MODELTRANS_FALLBACK = {
#            'default': (LANGUAGE_CODE, ),
#            'fy': ('nl', 'en')
#         }
#     """
#     MODELTRANS_FALLBACK = get_modeltrans_setting("MODELTRANS_FALLBACK")
# 
#     if lang not in MODELTRANS_FALLBACK.keys():
#         lang = "default"
# 
#     return MODELTRANS_FALLBACK[lang]
# 
# def get_modeltrans_setting(key):
#     modeltrans_settings = {
#         "MODELTRANS_AVAILABLE_LANGUAGES": get_available_languages(),
#         "MODELTRANS_FALLBACK": getattr(
#             settings, "MODELTRANS_FALLBACK", {"default": (get_default_language(),)}
#         ),
#         "MODELTRANS_ADD_FIELD_HELP_TEXT": getattr(settings, "MODELTRANS_ADD_FIELD_HELP_TEXT", True),
#     }
#     return modeltrans_settings.get(key)
# 
# Path: modeltrans/utils.py
# class FallbackTransform(Transform):
#     """
#     Custom version of KeyTextTransform to use a database field as part of the key.
# 
#     For example: with default_language="nl", calling
#     `FallbackTransform("title_", F("fallback_language"), "i18n")` becomes in SQL"
#         `"i18n"->>('title_' || "fallback_language")`
#     """
# 
#     def __init__(self, field_prefix, language_expression, *args, **kwargs):
#         super().__init__(*args, **kwargs)
#         self.field_prefix = field_prefix
#         self.language_expression = language_expression
# 
#     def preprocess_lhs(self, compiler, connection, lhs_only=False):
#         if not lhs_only:
#             key_transforms = [self.field_prefix]
#         previous = self.lhs
#         while isinstance(previous, KeyTransform):
#             if not lhs_only:
#                 key_transforms.insert(0, previous.key_name)
#             previous = previous.lhs
#         lhs, params = compiler.compile(previous)
#         return (lhs, params, key_transforms) if not lhs_only else (lhs, params)
# 
#     def as_postgresql(self, compiler, connection):
#         lhs, params, key_transforms = self.preprocess_lhs(compiler, connection)
#         params.extend([self.field_prefix])
# 
#         rhs = self.language_expression.resolve_expression(compiler.query)
#         rhs_sql, rhs_params = compiler.compile(rhs)
#         params.extend(rhs_params)
# 
#         return "(%s ->> (%%s || %s ))" % (lhs, rhs_sql), (params)
# 
# def build_localized_fieldname(field_name, lang, ignore_default=False):
#     if lang == "id":
#         # The 2-letter Indonesian language code is problematic with the
#         # current naming scheme as Django foreign keys also add "id" suffix.
#         lang = "ind"
#     if ignore_default and lang == get_default_language():
#         return field_name
#     return "{}_{}".format(field_name, lang.replace("-", "_"))
# 
# def get_instance_field_value(instance, path):
#     """
#     Return the value of a field or related field for a model instance.
#     """
#     value = instance
#     for bit in path.split(LOOKUP_SEP):
#         try:
#             value = getattr(value, bit)
#         except AttributeError:
#             return None
# 
#     return value
# 
# def get_language():
#     """
#     Return an active language code that is guaranteed to be in settings.LANGUAGES
# 
#     (Django does not seem to guarantee this for us.)
#     """
#     lang = _get_language()
#     if lang in get_available_languages():
#         return lang
#     return get_default_language()


from django.core.exceptions import ImproperlyConfigured
from django.db.models import F, fields
from django.db.models.functions import Cast, Coalesce
from django.utils.translation import gettext_lazy as _
from .conf import get_default_language, get_fallback_chain, get_modeltrans_setting
from .utils import (
    FallbackTransform,
    build_localized_fieldname,
    get_instance_field_value,
    get_language,
)
    from django.db.models import JSONField
    from django.db.models.fields.json import KeyTextTransform
    from django.contrib.postgres.fields import JSONField
    from django.contrib.postgres.fields.jsonb import KeyTextTransform


try:
    # django==3.1 moved JSONField into django.db.models
except ImportError:


SUPPORTED_FIELDS = (fields.CharField, fields.TextField)


# Path: modeltrans/conf.py
# def get_default_language():
#     return settings.LANGUAGE_CODE
# 
# def get_fallback_chain(lang):
#     """
#     Returns the list of fallback languages for language `lang`.
# 
#     For example, this function will return `('nl', 'en')` when called
#     with `lang='fy'` and configured like this::
# 
#         LANGUAGE_CODE = 'en'
# 
#         MODELTRANS_FALLBACK = {
#            'default': (LANGUAGE_CODE, ),
#            'fy': ('nl', 'en')
#         }
#     """
#     MODELTRANS_FALLBACK = get_modeltrans_setting("MODELTRANS_FALLBACK")
# 
#     if lang not in MODELTRANS_FALLBACK.keys():
#         lang = "default"
# 
#     return MODELTRANS_FALLBACK[lang]
# 
# def get_modeltrans_setting(key):
#     modeltrans_settings = {
#         "MODELTRANS_AVAILABLE_LANGUAGES": get_available_languages(),
#         "MODELTRANS_FALLBACK": getattr(
#             settings, "MODELTRANS_FALLBACK", {"default": (get_default_language(),)}
#         ),
#         "MODELTRANS_ADD_FIELD_HELP_TEXT": getattr(settings, "MODELTRANS_ADD_FIELD_HELP_TEXT", True),
#     }
#     return modeltrans_settings.get(key)
# 
# Path: modeltrans/utils.py
# class FallbackTransform(Transform):
#     """
#     Custom version of KeyTextTransform to use a database field as part of the key.
# 
#     For example: with default_language="nl", calling
#     `FallbackTransform("title_", F("fallback_language"), "i18n")` becomes in SQL"
#         `"i18n"->>('title_' || "fallback_language")`
#     """
# 
#     def __init__(self, field_prefix, language_expression, *args, **kwargs):
#         super().__init__(*args, **kwargs)
#         self.field_prefix = field_prefix
#         self.language_expression = language_expression
# 
#     def preprocess_lhs(self, compiler, connection, lhs_only=False):
#         if not lhs_only:
#             key_transforms = [self.field_prefix]
#         previous = self.lhs
#         while isinstance(previous, KeyTransform):
#             if not lhs_only:
#                 key_transforms.insert(0, previous.key_name)
#             previous = previous.lhs
#         lhs, params = compiler.compile(previous)
#         return (lhs, params, key_transforms) if not lhs_only else (lhs, params)
# 
#     def as_postgresql(self, compiler, connection):
#         lhs, params, key_transforms = self.preprocess_lhs(compiler, connection)
#         params.extend([self.field_prefix])
# 
#         rhs = self.language_expression.resolve_expression(compiler.query)
#         rhs_sql, rhs_params = compiler.compile(rhs)
#         params.extend(rhs_params)
# 
#         return "(%s ->> (%%s || %s ))" % (lhs, rhs_sql), (params)
# 
# def build_localized_fieldname(field_name, lang, ignore_default=False):
#     if lang == "id":
#         # The 2-letter Indonesian language code is problematic with the
#         # current naming scheme as Django foreign keys also add "id" suffix.
#         lang = "ind"
#     if ignore_default and lang == get_default_language():
#         return field_name
#     return "{}_{}".format(field_name, lang.replace("-", "_"))
# 
# def get_instance_field_value(instance, path):
#     """
#     Return the value of a field or related field for a model instance.
#     """
#     value = instance
#     for bit in path.split(LOOKUP_SEP):
#         try:
#             value = getattr(value, bit)
#         except AttributeError:
#             return None
# 
#     return value
# 
# def get_language():
#     """
#     Return an active language code that is guaranteed to be in settings.LANGUAGES
# 
#     (Django does not seem to guarantee this for us.)
#     """
#     lang = _get_language()
#     if lang in get_available_languages():
#         return lang
#     return get_default_language()


# Path: modeltrans/fields.py
from django.core.exceptions import ImproperlyConfigured
from django.db.models import F, fields
from django.db.models.functions import Cast, Coalesce
from django.utils.translation import gettext_lazy as _
from .conf import get_default_language, get_fallback_chain, get_modeltrans_setting
from .utils import (
    FallbackTransform,
    build_localized_fieldname,
    get_instance_field_value,
    get_language,
)
    from django.db.models import JSONField
    from django.db.models.fields.json import KeyTextTransform
    from django.contrib.postgres.fields import JSONField
    from django.contrib.postgres.fields.jsonb import KeyTextTransform



try:
    # django==3.1 moved JSONField into django.db.models
except ImportError:


SUPPORTED_FIELDS = (fields.CharField, fields.TextField)


DEFAULT_LANGUAGE = get_default_language()