"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""

from __future__ import annotations
from .apikeyauth import APIKeyAuth, APIKeyAuthTypedDict
from .oauth2tokenauth import OAuth2TokenAuth, OAuth2TokenAuthTypedDict
from .toolconfiguration import ToolConfiguration, ToolConfigurationTypedDict
from mistralai.azure.client.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from mistralai.azure.client.utils import validate_const
import pydantic
from pydantic import Field, model_serializer
from pydantic.functional_validators import AfterValidator
from typing import Literal, Union
from typing_extensions import Annotated, NotRequired, TypeAliasType, TypedDict


AuthorizationTypedDict = TypeAliasType(
    "AuthorizationTypedDict", Union[OAuth2TokenAuthTypedDict, APIKeyAuthTypedDict]
)


Authorization = Annotated[
    Union[APIKeyAuth, OAuth2TokenAuth], Field(discriminator="type")
]


class CustomConnectorTypedDict(TypedDict):
    connector_id: str
    type: Literal["connector"]
    authorization: NotRequired[Nullable[AuthorizationTypedDict]]
    tool_configuration: NotRequired[Nullable[ToolConfigurationTypedDict]]


class CustomConnector(BaseModel):
    connector_id: str

    type: Annotated[
        Annotated[Literal["connector"], AfterValidator(validate_const("connector"))],
        pydantic.Field(alias="type"),
    ] = "connector"

    authorization: OptionalNullable[Authorization] = UNSET

    tool_configuration: OptionalNullable[ToolConfiguration] = UNSET

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(["authorization", "tool_configuration"])
        nullable_fields = set(["authorization", "tool_configuration"])
        serialized = handler(self)
        m = {}

        for n, f in type(self).model_fields.items():
            k = f.alias or n
            val = serialized.get(k, serialized.get(n))
            is_nullable_and_explicitly_set = (
                k in nullable_fields
                and (self.__pydantic_fields_set__.intersection({n}))  # pylint: disable=no-member
            )

            if val != UNSET_SENTINEL:
                if (
                    val is not None
                    or k not in optional_fields
                    or is_nullable_and_explicitly_set
                ):
                    m[k] = val

        return m


try:
    CustomConnector.model_rebuild()
except NameError:
    pass
