""" This module holds the ``i18n_utils`` template tags of the ``translation`` app. """

import os

from django import template
from django.conf import settings
from mtp.apps.translation import utils

register = template.Library()


@register.simple_tag
def get_available_languages():
    return utils.get_available_languages()


@register.simple_tag
def get_country_flag(code):
    """Get the path of the country flag image from the provided language code.

    Returns
    -------
    str
        The path of the country flag.
        If the file not exists, None is returned.

    """

    if '-' in code:
        code = code.split('-', 1)[1].lower()

    flag_path = 'translation/flags/%s.png' % code

    static_path = os.path.join(settings.APPS_ROOT, 'translation/static')

    if os.path.exists(os.path.join(static_path, flag_path)):
        return os.path.join(settings.STATIC_URL, flag_path)
    else:
        return None
