"""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
import pydantic
from pydantic import Discriminator, Tag, model_serializer
from pydantic.functional_validators import AfterValidator
from syllable_sdk.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from syllable_sdk.utils import get_discriminator, validate_const
from typing import Any, Literal, Optional, Union
from typing_extensions import Annotated, NotRequired, TypeAliasType, TypedDict


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


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


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


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


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


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


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


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


class SetValueActionTypedDict(TypedDict):
    name: str
    r"""Destination path to mutate (e.g. output.foo)."""
    value: NotRequired[Nullable[Any]]
    r"""Initial value of the variable."""
    value_from: NotRequired[Nullable[SetValueActionValueFrom2TypedDict]]
    r"""Expression to compute initial value (mutually exclusive with value)."""
    if_: NotRequired[Nullable[SetValueActionIf2TypedDict]]
    r"""An expression that must evaluate to true for the action to be applied."""
    action: Literal["set"]


class SetValueAction(BaseModel):
    name: str
    r"""Destination path to mutate (e.g. output.foo)."""

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

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

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

    ACTION: Annotated[
        Annotated[Optional[Literal["set"]], AfterValidator(validate_const("set"))],
        pydantic.Field(alias="action"),
    ] = "set"

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