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

from __future__ import annotations
from .celexpression import CelExpression, CelExpressionTypedDict
from .jmespathexpression import JMESPathExpression, JMESPathExpressionTypedDict
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, TYPE_CHECKING, Union
from typing_extensions import Annotated, NotRequired, TypeAliasType, TypedDict

if TYPE_CHECKING:
    from .caseexpression import CaseExpression, CaseExpressionTypedDict


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


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


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


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


ConditionalValueIf2TypedDict = TypeAliasType(
    "ConditionalValueIf2TypedDict",
    Union["CaseExpressionTypedDict", ConditionalValueIf1TypedDict, str],
)
r"""An expression that must evaluate to true for the action to be applied."""


ConditionalValueIf2 = TypeAliasType(
    "ConditionalValueIf2", Union["CaseExpression", ConditionalValueIf1, str]
)
r"""An expression that must evaluate to true for the action to be applied."""


class ConditionalValueTypedDict(TypedDict):
    value: NotRequired[Nullable[Any]]
    r"""Initial value of the variable."""
    value_from: NotRequired[Nullable[ConditionalValueValueFrom2TypedDict]]
    r"""Expression to compute initial value (mutually exclusive with value)."""
    if_: NotRequired[Nullable[ConditionalValueIf2TypedDict]]
    r"""An expression that must evaluate to true for the action to be applied."""


class ConditionalValue(BaseModel):
    value: OptionalNullable[Any] = UNSET
    r"""Initial value of the variable."""

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

    if_: Annotated[
        OptionalNullable[ConditionalValueIf2], pydantic.Field(alias="if")
    ] = UNSET
    r"""An expression that must evaluate to true for the action to be applied."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(["value", "value_from", "if"])
        nullable_fields = set(["value", "value_from", "if"])
        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


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


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