# pylint: disable=unused-import
from django.utils.functional import Promise, lazy

from wagtail import hooks

from ocyan.core.fender import config

# this is only loaded to make sure the adapters are registered
from . import adapters, patches

# localize_menu_urls is not needed if i18n is not enabled.
if config.i18n_enabled:
    # pylint: disable=import-error,no-name-in-module
    from ocyan.i18n.templatetags.i18n_helpers import translated_url

    lazy_translated_url = lazy(translated_url, str)

    # if wagtail would have registered the urls with reverse_lazy this
    # would not be needed. But now we need to translate the url with the
    # i18n_helper ourselved.
    @hooks.register("construct_reports_menu")
    @hooks.register("construct_settings_menu")
    @hooks.register("construct_main_menu")
    def localize_menu_urls(request, menuitems):
        for menuitem in menuitems:
            if not isinstance(menuitem.url, Promise):
                menuitem.url = lazy_translated_url(menuitem.url)

        return menuitems
