"""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 syllable_sdk.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from syllable_sdk.utils import get_discriminator
from typing import List, Union
from typing_extensions import Annotated, NotRequired, TypeAliasType, TypedDict


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


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


NextStepIf2TypedDict = TypeAliasType(
    "NextStepIf2TypedDict", Union[CaseExpressionTypedDict, NextStepIf1TypedDict, str]
)
r"""Condition to decide whether this item executes. Supported expression forms: (1) JMESPath string (default for plain strings), (2) typed JMESPath object {\"type\":\"jp\"|\"jmespath\",\"expression\":\"...\"}, or (3) typed CEL object {\"type\":\"cel\",\"expression\":\"...\"}. Example JMESPath string: \"inputs.can_sign_consent == `true`\"."""


NextStepIf2 = TypeAliasType("NextStepIf2", Union[CaseExpression, NextStepIf1, str])
r"""Condition to decide whether this item executes. Supported expression forms: (1) JMESPath string (default for plain strings), (2) typed JMESPath object {\"type\":\"jp\"|\"jmespath\",\"expression\":\"...\"}, or (3) typed CEL object {\"type\":\"cel\",\"expression\":\"...\"}. Example JMESPath string: \"inputs.can_sign_consent == `true`\"."""


class NextStepTypedDict(TypedDict):
    r"""Represents a conditional transition to the next step."""

    id: str
    r"""The identifier of the next step."""
    if_: NotRequired[Nullable[NextStepIf2TypedDict]]
    r"""Condition to decide whether this item executes. Supported expression forms: (1) JMESPath string (default for plain strings), (2) typed JMESPath object {\"type\":\"jp\"|\"jmespath\",\"expression\":\"...\"}, or (3) typed CEL object {\"type\":\"cel\",\"expression\":\"...\"}. Example JMESPath string: \"inputs.can_sign_consent == `true`\"."""
    requires: NotRequired[Nullable[List[str]]]
    r"""List of input field names required for this transition. Validates that specified inputs are collected before allowing transition."""


class NextStep(BaseModel):
    r"""Represents a conditional transition to the next step."""

    id: str
    r"""The identifier of the next step."""

    if_: Annotated[OptionalNullable[NextStepIf2], pydantic.Field(alias="if")] = UNSET
    r"""Condition to decide whether this item executes. Supported expression forms: (1) JMESPath string (default for plain strings), (2) typed JMESPath object {\"type\":\"jp\"|\"jmespath\",\"expression\":\"...\"}, or (3) typed CEL object {\"type\":\"cel\",\"expression\":\"...\"}. Example JMESPath string: \"inputs.can_sign_consent == `true`\"."""

    requires: OptionalNullable[List[str]] = UNSET
    r"""List of input field names required for this transition. Validates that specified inputs are collected before allowing transition."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(["if", "requires"])
        nullable_fields = set(["if", "requires"])
        serialized = handler(self)
        m = {}

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


try:
    NextStep.model_rebuild()
except NameError:
    pass
