from wagtail.contrib.settings.templatetags.wagtailsettings_tags import (
    SettingsNode,
    register,
)

from ocyan.main.loading import get_ocyan_class

get_wagtailsettings = get_ocyan_class("wagtail_utils.utils", "get_wagtailsettings")


@register.simple_tag(takes_context=True)
def get_settings(context, use_default_site=False):
    try:
        context["settings"] = SettingsNode.get_settings_object(
            context, use_default_site
        )
    except RuntimeError:
        if not "site" in context:
            raise
        context["settings"] = get_wagtailsettings(context["site"].domain)
    return ""
