import functools

from pyidentity.security.authorization.abc import IAuthorizationPolicyProvider
from pyidentity.security.authorization.options import AuthorizationOptions
from pyidentity.security.authorization.policy import AuthorizationPolicy

__all__ = ("AuthorizationPolicyProvider",)


class AuthorizationPolicyProvider(IAuthorizationPolicyProvider):
    """The default implementation of a policy provider, which provides a *AuthorizationPolicy* for a particular name."""

    __slots__ = ("_options",)

    def __init__(self, options: AuthorizationOptions) -> None:
        self._options = options

    @functools.cache
    def get_policy(self, name: str) -> AuthorizationPolicy | None:
        """Gets a *AuthorizationPolicy* from the given name.

        :param name: The policy name to retrieve.
        :return:
        """
        return self._options.get_policy(name)

    @functools.cache
    def get_default_policy(self) -> AuthorizationPolicy | None:
        """Gets the default authorization policy."""
        return self._options.default_policy
