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

from __future__ import annotations
from .schema2_union_1 import (
    Schema2Checkbox,
    Schema2CheckboxTypedDict,
    Schema2Date,
    Schema2DateTypedDict,
    Schema2File,
    Schema2FileTypedDict,
    Schema2MultiSelect,
    Schema2MultiSelectTypedDict,
    Schema2Number,
    Schema2NumberTypedDict,
    Schema2SingleSelect,
    Schema2SingleSelectTypedDict,
    Schema2Text,
    Schema2TextTypedDict,
    Schema2UnifiedKey6,
    Schema2UnifiedKey7,
)
from kombo.types import BaseModel, Nullable, OptionalNullable, UNSET, UNSET_SENTINEL
from kombo.utils import validate_const
import pydantic
from pydantic import model_serializer
from pydantic.functional_validators import AfterValidator
from typing import Dict, Literal, TYPE_CHECKING, Union
from typing_extensions import Annotated, NotRequired, TypeAliasType, TypedDict

if TYPE_CHECKING:
    from .schema1_union_2 import Schema1Union2, Schema1Union2TypedDict


class Schema2Object2TypedDict(TypedDict):
    label: str
    required: bool
    properties: Dict[str, "Schema1Union2TypedDict"]
    description: NotRequired[Nullable[str]]
    unified_key: NotRequired[Nullable[Schema2UnifiedKey6]]
    type: Literal["object"]


class Schema2Object2(BaseModel):
    label: str

    required: bool

    properties: Dict[str, "Schema1Union2"]

    description: OptionalNullable[str] = UNSET

    unified_key: OptionalNullable[Schema2UnifiedKey6] = UNSET

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

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = ["description", "unified_key"]
        nullable_fields = ["description", "unified_key"]
        null_default_fields = []

        serialized = handler(self)

        m = {}

        for n, f in type(self).model_fields.items():
            k = f.alias or n
            val = serialized.get(k)
            serialized.pop(k, None)

            optional_nullable = k in optional_fields and k in nullable_fields
            is_set = (
                self.__pydantic_fields_set__.intersection({n})
                or k in null_default_fields
            )  # pylint: disable=no-member

            if val is not None and val != UNSET_SENTINEL:
                m[k] = val
            elif val != UNSET_SENTINEL and (
                not k in optional_fields or (optional_nullable and is_set)
            ):
                m[k] = val

        return m


class Schema2Array2TypedDict(TypedDict):
    label: str
    required: bool
    item_type: Schema2Union2TypedDict
    description: NotRequired[Nullable[str]]
    unified_key: NotRequired[Nullable[Schema2UnifiedKey7]]
    type: Literal["array"]
    min_items: NotRequired[Nullable[float]]
    max_items: NotRequired[Nullable[float]]


class Schema2Array2(BaseModel):
    label: str

    required: bool

    item_type: Schema2Union2

    description: OptionalNullable[str] = UNSET

    unified_key: OptionalNullable[Schema2UnifiedKey7] = UNSET

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

    min_items: OptionalNullable[float] = UNSET

    max_items: OptionalNullable[float] = UNSET

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = ["description", "unified_key", "min_items", "max_items"]
        nullable_fields = ["description", "unified_key", "min_items", "max_items"]
        null_default_fields = []

        serialized = handler(self)

        m = {}

        for n, f in type(self).model_fields.items():
            k = f.alias or n
            val = serialized.get(k)
            serialized.pop(k, None)

            optional_nullable = k in optional_fields and k in nullable_fields
            is_set = (
                self.__pydantic_fields_set__.intersection({n})
                or k in null_default_fields
            )  # pylint: disable=no-member

            if val is not None and val != UNSET_SENTINEL:
                m[k] = val
            elif val != UNSET_SENTINEL and (
                not k in optional_fields or (optional_nullable and is_set)
            ):
                m[k] = val

        return m


Schema2Union2TypedDict = TypeAliasType(
    "Schema2Union2TypedDict",
    Union[
        Schema2DateTypedDict,
        Schema2CheckboxTypedDict,
        Schema2SingleSelectTypedDict,
        "Schema2Object2TypedDict",
        Schema2FileTypedDict,
        Schema2NumberTypedDict,
        Schema2TextTypedDict,
        Schema2MultiSelectTypedDict,
        "Schema2Array2TypedDict",
    ],
)


Schema2Union2 = TypeAliasType(
    "Schema2Union2",
    Union[
        Schema2Date,
        Schema2Checkbox,
        Schema2SingleSelect,
        "Schema2Object2",
        Schema2File,
        Schema2Number,
        Schema2Text,
        Schema2MultiSelect,
        "Schema2Array2",
    ],
)
