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

from __future__ import annotations
from .googlepaypaymentoptioncontext import (
    GooglePayPaymentOptionContext,
    GooglePayPaymentOptionContextTypedDict,
)
from .mode import Mode
from .paymentoptioncontext import PaymentOptionContext, PaymentOptionContextTypedDict
from .walletpaymentoptioncontext import (
    WalletPaymentOptionContext,
    WalletPaymentOptionContextTypedDict,
)
from gr4vy.types import BaseModel, Nullable, OptionalNullable, UNSET, UNSET_SENTINEL
from gr4vy.utils import validate_const
import pydantic
from pydantic import model_serializer
from pydantic.functional_validators import AfterValidator
from typing import Literal, Optional, Union
from typing_extensions import Annotated, NotRequired, TypeAliasType, TypedDict


ContextTypedDict = TypeAliasType(
    "ContextTypedDict",
    Union[
        WalletPaymentOptionContextTypedDict,
        GooglePayPaymentOptionContextTypedDict,
        PaymentOptionContextTypedDict,
    ],
)


Context = TypeAliasType(
    "Context",
    Union[
        WalletPaymentOptionContext, GooglePayPaymentOptionContext, PaymentOptionContext
    ],
)


class PaymentOptionTypedDict(TypedDict):
    method: str
    mode: Mode
    can_store_payment_method: bool
    can_delay_capture: bool
    type: Literal["payment-option"]
    icon_url: NotRequired[Nullable[str]]
    label: NotRequired[Nullable[str]]
    context: NotRequired[Nullable[ContextTypedDict]]


class PaymentOption(BaseModel):
    method: str

    mode: Mode

    can_store_payment_method: bool

    can_delay_capture: bool

    TYPE: Annotated[
        Annotated[
            Optional[Literal["payment-option"]],
            AfterValidator(validate_const("payment-option")),
        ],
        pydantic.Field(alias="type"),
    ] = "payment-option"

    icon_url: OptionalNullable[str] = UNSET

    label: OptionalNullable[str] = UNSET

    context: OptionalNullable[Context] = UNSET

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