from django import template
from django.utils.translation import override

from ocyan.core.fender import config

register = template.Library()


@register.simple_tag(takes_context=True)
def get_hreflang_url(context, page):
    """
    By default, the get_full_url doesn't add the language prefix in front of the url.
    In our page model, we do the following in get_url_parts:
    with override(self.language.code):
        return super().get_url_parts(request)
    This templatetags also overrides the language so we can get the correct url for hreflang meta tag.
    """
    request = context["request"]

    if config.i18n_enabled:
        with override(page.language.code):
            return page.get_full_url(request)

    return page.get_full_url(request)
