# pylint: skip-file
from ocyan.core.fender import config

# pylint: disable=no-member
if "ocyan.plugin.wagtail" in config.ocyan_plugins:
    from django.utils.translation import gettext_lazy as _

    from wagtail.models import Page, PageManager
    from wagtail.query import PageQuerySet

    class BrowsablePageQueryset(PageQuerySet):
        def browsable(self):
            return self.live()

        def for_menu(self):
            return self.browsable()

    BrowsablePageManager = PageManager.from_queryset(BrowsablePageQueryset)

    class PageMenuProxy(Page):
        objects = BrowsablePageManager()
        _slug_separator = "/"

        is_creatable = False

        def get_full_slug(self, _=None):
            return self.specific.get_url(
                request=getattr(self.__class__, "_request", None),
                current_site=getattr(self.__class__, "_site", None),
            )

        @property
        def name(self):
            return self.title

        # pylint: disable=unused-argument
        def _get_absolute_url(self, parent_slug=None):
            return self.get_absolute_url()

        def get_absolute_url(self, _=None):
            return self.get_full_slug()

        def get_ancestors(self, inclusive=False):
            return self.__class__.objects.ancestor_of(self, inclusive).live().in_menu()

        def get_descendants(self, inclusive=False):
            return (
                self.__class__.objects.descendant_of(self, inclusive).live().in_menu()
            )

        def get_siblings(self, inclusive=True):
            return self.__class__.objects.sibling_of(self, inclusive).live().in_menu()

        @classmethod
        def from_page(cls, page, request=None, site=None):
            cls._request = request
            cls._site = site
            return cls.objects.get(pk=page.pk)

        class Meta(Page.Meta):
            proxy = True
            verbose_name = _("Empty page")
            verbose_name_plural = _("Empty pages")
            managed = False
