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

from __future__ import annotations
from .eventtask import EventTask, EventTaskTypedDict
from .expressiontask import ExpressionTask, ExpressionTaskTypedDict
from .loadtoolfromfiletask import LoadToolFromFileTask, LoadToolFromFileTaskTypedDict
from .stepstask import StepsTask, StepsTaskTypedDict
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


Task2TypedDict = TypeAliasType(
    "Task2TypedDict",
    Union[
        LoadToolFromFileTaskTypedDict,
        EventTaskTypedDict,
        StepsTaskTypedDict,
        ExpressionTaskTypedDict,
    ],
)


Task2 = Annotated[
    Union[
        Annotated[EventTask, Tag("event")],
        Annotated[ExpressionTask, Tag("expression")],
        Annotated[LoadToolFromFileTask, Tag("import")],
        Annotated[StepsTask, Tag("steps")],
    ],
    Discriminator(lambda m: get_discriminator(m, "type", "type")),
]


Task1TypedDict = TypeAliasType(
    "Task1TypedDict",
    Union[
        LoadToolFromFileTaskTypedDict,
        EventTaskTypedDict,
        StepsTaskTypedDict,
        ExpressionTaskTypedDict,
    ],
)


Task1 = Annotated[
    Union[
        Annotated[EventTask, Tag("event")],
        Annotated[ExpressionTask, Tag("expression")],
        Annotated[LoadToolFromFileTask, Tag("import")],
        Annotated[StepsTask, Tag("steps")],
    ],
    Discriminator(lambda m: get_discriminator(m, "type", "type")),
]


Task3TypedDict = TypeAliasType(
    "Task3TypedDict", Union[Task1TypedDict, List[Task2TypedDict]]
)
r"""Task implementation details"""


Task3 = TypeAliasType("Task3", Union[Task1, List[Task2]])
r"""Task implementation details"""


class ContextTypedDict(TypedDict):
    task: NotRequired[Nullable[Task3TypedDict]]
    r"""Task implementation details"""


class Context(BaseModel):
    task: OptionalNullable[Task3] = UNSET
    r"""Task implementation details"""

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