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

from __future__ import annotations
from .websearchsource import WebSearchSource, WebSearchSourceTypedDict
from .websearchstatus import WebSearchStatus
from openrouter.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from openrouter.utils import get_discriminator, validate_open_enum
from pydantic import Discriminator, Tag, model_serializer
from pydantic.functional_validators import PlainValidator
from typing import List, Literal, Optional, Union
from typing_extensions import Annotated, NotRequired, TypeAliasType, TypedDict


TypeFindInPage = Literal["find_in_page",]


class ActionFindInPageTypedDict(TypedDict):
    pattern: str
    type: TypeFindInPage
    url: str


class ActionFindInPage(BaseModel):
    pattern: str

    type: TypeFindInPage

    url: str


TypeOpenPage = Literal["open_page",]


class ActionOpenPageTypedDict(TypedDict):
    type: TypeOpenPage
    url: NotRequired[Nullable[str]]


class ActionOpenPage(BaseModel):
    type: TypeOpenPage

    url: OptionalNullable[str] = UNSET

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = ["url"]
        nullable_fields = ["url"]
        null_default_fields = []

        serialized = handler(self)

        m = {}

        for n, f in type(self).model_fields.items():
            k = f.alias or n
            val = serialized.get(k)
            serialized.pop(k, None)

            optional_nullable = k in optional_fields and k in nullable_fields
            is_set = (
                self.__pydantic_fields_set__.intersection({n})
                or k in null_default_fields
            )  # pylint: disable=no-member

            if val is not None and val != UNSET_SENTINEL:
                m[k] = val
            elif val != UNSET_SENTINEL and (
                not k in optional_fields or (optional_nullable and is_set)
            ):
                m[k] = val

        return m


ActionTypeSearch = Literal["search",]


class OutputWebSearchCallItemActionSearchTypedDict(TypedDict):
    query: str
    type: ActionTypeSearch
    queries: NotRequired[List[str]]
    sources: NotRequired[List[WebSearchSourceTypedDict]]


class OutputWebSearchCallItemActionSearch(BaseModel):
    query: str

    type: ActionTypeSearch

    queries: Optional[List[str]] = None

    sources: Optional[List[WebSearchSource]] = None


ActionTypedDict = TypeAliasType(
    "ActionTypedDict",
    Union[
        ActionOpenPageTypedDict,
        ActionFindInPageTypedDict,
        OutputWebSearchCallItemActionSearchTypedDict,
    ],
)


Action = Annotated[
    Union[
        Annotated[OutputWebSearchCallItemActionSearch, Tag("search")],
        Annotated[ActionOpenPage, Tag("open_page")],
        Annotated[ActionFindInPage, Tag("find_in_page")],
    ],
    Discriminator(lambda m: get_discriminator(m, "type", "type")),
]


TypeWebSearchCall = Literal["web_search_call",]


class OutputWebSearchCallItemTypedDict(TypedDict):
    id: str
    status: WebSearchStatus
    type: TypeWebSearchCall
    action: NotRequired[ActionTypedDict]


class OutputWebSearchCallItem(BaseModel):
    id: str

    status: Annotated[WebSearchStatus, PlainValidator(validate_open_enum(False))]

    type: TypeWebSearchCall

    action: Optional[Action] = None
