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

from __future__ import annotations
from .caseexpression import CaseExpression, CaseExpressionTypedDict
from .celexpression import CelExpression, CelExpressionTypedDict
from .jmespathexpression import JMESPathExpression, JMESPathExpressionTypedDict
from enum import Enum
import pydantic
from pydantic import Discriminator, Tag, model_serializer
from syllable_sdk.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from syllable_sdk.utils import get_discriminator
from typing import Any, List, Union
from typing_extensions import Annotated, NotRequired, TypeAliasType, TypedDict


VariableValueFrom1TypedDict = TypeAliasType(
    "VariableValueFrom1TypedDict",
    Union[CelExpressionTypedDict, JMESPathExpressionTypedDict],
)


VariableValueFrom1 = Annotated[
    Union[
        Annotated[CelExpression, Tag("cel")],
        Annotated[JMESPathExpression, Tag("jmespath")],
        Annotated[JMESPathExpression, Tag("jp")],
    ],
    Discriminator(lambda m: get_discriminator(m, "type", "type")),
]


VariableValueFrom2TypedDict = TypeAliasType(
    "VariableValueFrom2TypedDict",
    Union[CaseExpressionTypedDict, VariableValueFrom1TypedDict, str],
)
r"""Expression to compute initial value (mutually exclusive with value)."""


VariableValueFrom2 = TypeAliasType(
    "VariableValueFrom2", Union[CaseExpression, VariableValueFrom1, str]
)
r"""Expression to compute initial value (mutually exclusive with value)."""


class VariableType(str, Enum):
    STRING = "string"
    NUMBER = "number"
    INTEGER = "integer"
    BOOLEAN = "boolean"
    OBJECT = "object"
    ARRAY = "array"
    NULL = "null"


class VariableTypedDict(TypedDict):
    name: str
    r"""The name of the property."""
    value: NotRequired[Nullable[Any]]
    r"""Initial value of the variable."""
    value_from: NotRequired[Nullable[VariableValueFrom2TypedDict]]
    r"""Expression to compute initial value (mutually exclusive with value)."""
    type: NotRequired[Nullable[VariableType]]
    description: NotRequired[Nullable[str]]
    title: NotRequired[Nullable[str]]
    format_: NotRequired[Nullable[str]]
    pattern: NotRequired[Nullable[str]]
    enum: NotRequired[Nullable[List[str]]]
    examples: NotRequired[Nullable[List[Any]]]


class Variable(BaseModel):
    name: str
    r"""The name of the property."""

    value: OptionalNullable[Any] = UNSET
    r"""Initial value of the variable."""

    value_from: OptionalNullable[VariableValueFrom2] = UNSET
    r"""Expression to compute initial value (mutually exclusive with value)."""

    type: OptionalNullable[VariableType] = UNSET

    description: OptionalNullable[str] = UNSET

    title: OptionalNullable[str] = UNSET

    format_: Annotated[OptionalNullable[str], pydantic.Field(alias="format")] = UNSET

    pattern: OptionalNullable[str] = UNSET

    enum: OptionalNullable[List[str]] = UNSET

    examples: OptionalNullable[List[Any]] = UNSET

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(
            [
                "value",
                "value_from",
                "type",
                "description",
                "title",
                "format",
                "pattern",
                "enum",
                "examples",
            ]
        )
        nullable_fields = set(
            [
                "value",
                "value_from",
                "type",
                "description",
                "title",
                "format",
                "pattern",
                "enum",
                "examples",
            ]
        )
        serialized = handler(self)
        m = {}

        for n, f in type(self).model_fields.items():
            k = f.alias or n
            val = serialized.get(k)
            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
