"""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, Optional, Union
from typing_extensions import Annotated, NotRequired, TypeAliasType, TypedDict


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


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


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


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


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


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


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


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


class GetValueActionAction(str, Enum):
    r"""Populate default input values."""

    GET = "get"
    LOAD = "load"


class GetValueActionTypedDict(TypedDict):
    value: NotRequired[Nullable[Any]]
    r"""Initial value of the variable."""
    value_from: NotRequired[Nullable[GetValueActionValueFrom2TypedDict]]
    r"""Expression to compute initial value (mutually exclusive with value)."""
    if_: NotRequired[Nullable[GetValueActionIf2TypedDict]]
    r"""An expression that must evaluate to true for the action to be applied."""
    action: NotRequired[GetValueActionAction]
    r"""Populate default input values."""
    inputs: NotRequired[Nullable[List[str]]]
    r"""Input field names to populate; None populates all step inputs."""
    overwrite: NotRequired[bool]
    r"""If False (default), only populate empty inputs. If True, always overwrite."""


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

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

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

    action: Optional[GetValueActionAction] = GetValueActionAction.GET
    r"""Populate default input values."""

    inputs: OptionalNullable[List[str]] = UNSET
    r"""Input field names to populate; None populates all step inputs."""

    overwrite: Optional[bool] = False
    r"""If False (default), only populate empty inputs. If True, always overwrite."""

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