from functools import wraps

from datasette import Forbidden
from datasette_plugin_router import Router

router = Router()

SIDEBAR_ACCESS_ACTION = "datasette-sidebar-access"


def check_permission():
    """Decorator for routes requiring sidebar access."""

    def decorator(func):
        @wraps(func)
        async def wrapper(datasette, request, **kwargs):
            result = await datasette.allowed(
                action=SIDEBAR_ACCESS_ACTION, actor=request.actor
            )
            if not result:
                raise Forbidden("Permission denied for datasette-sidebar access")
            return await func(datasette=datasette, request=request, **kwargs)

        return wrapper

    return decorator
