from pyidentity.exc import ArgumentNullError, InvalidOperationError
from pyidentity.security.authentication.abc import IAuthenticationHandler
from pyidentity.security.authentication.scheme import AuthenticationScheme

__all__ = ("AuthenticationSchemeBuilder",)


class AuthenticationSchemeBuilder:
    __slots__ = ("_name", "display_name", "handler")

    def __init__(
        self,
        name: str,
        handler: IAuthenticationHandler | None = None,
        display_name: str | None = None,
    ) -> None:
        if name is None:
            raise ArgumentNullError("name")

        self._name = name
        self.handler = handler
        """Gets or sets the *IAuthenticationHandler* type responsible for this scheme."""
        self.display_name = display_name
        """Gets or sets the display name for the scheme being built."""

    @property
    def name(self) -> str:
        """Gets the name of the scheme being built."""
        return self._name

    def build(self) -> AuthenticationScheme:
        """Builds the *AuthenticationScheme* instance."""
        if self.handler is None:
            raise InvalidOperationError("'handler' must be configured to build an 'AuthenticationScheme'")

        return AuthenticationScheme(self.name, self.handler, self.display_name)
