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


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


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


IncrementActionIf2TypedDict = TypeAliasType(
    "IncrementActionIf2TypedDict",
    Union[CaseExpressionTypedDict, IncrementActionIf1TypedDict, 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`\"."""


IncrementActionIf2 = TypeAliasType(
    "IncrementActionIf2", Union[CaseExpression, IncrementActionIf1, 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 IncrementActionTypedDict(TypedDict):
    name: str
    r"""Numeric destination path to increment."""
    if_: NotRequired[Nullable[IncrementActionIf2TypedDict]]
    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`\"."""
    action: Literal["inc"]
    by: NotRequired[int]
    r"""Increment amount (defaults to 1)."""


class IncrementAction(BaseModel):
    name: str
    r"""Numeric destination path to increment."""

    if_: Annotated[OptionalNullable[IncrementActionIf2], 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`\"."""

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

    by: Optional[int] = 1
    r"""Increment amount (defaults to 1)."""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(["if", "action", "by"])
        nullable_fields = set(["if"])
        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:
    IncrementAction.model_rebuild()
except NameError:
    pass
