from django.contrib.admin.utils import quote
from django.urls import reverse_lazy
from django.utils.functional import keep_lazy


class LazyModelViewSetMixin:
    def get_edit_url(self, instance):
        if self.edit_url_name and self.user_has_permission("change"):
            return reverse_lazy(self.edit_url_name, args=(quote(instance.pk),))

    def get_copy_url(self, instance):
        if self.copy_url_name and self.user_has_permission("add"):
            return reverse_lazy(self.copy_url_name, args=(quote(instance.pk),))

    def get_inspect_url(self, instance):
        if self.inspect_url_name and self.user_has_any_permission(
            {"add", "change", "delete", "view"}
        ):
            return reverse_lazy(self.inspect_url_name, args=(quote(instance.pk),))

    def get_delete_url(self, instance):
        if self.delete_url_name and self.user_has_permission("delete"):
            return reverse_lazy(self.delete_url_name, args=(quote(instance.pk),))

    @keep_lazy(str)
    def _set_locale_query_param(self, url, locale=None):
        return super()._set_locale_query_param(url, locale)

    def get_add_url(self):
        if self.add_url_name and self.user_has_permission("add"):
            return self._set_locale_query_param(reverse_lazy(self.add_url_name))
