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

from __future__ import annotations
from postivo_client.types import (
    BaseModel,
    Nullable,
    OptionalNullable,
    UNSET,
    UNSET_SENTINEL,
)
from pydantic import model_serializer
from typing import Optional
from typing_extensions import NotRequired, TypedDict


class RecipientFromAddressBookByExternalIDTypedDict(TypedDict):
    r"""Select shipment recipient data from the Address Book by external (custom) ID."""

    ext_id: str
    r"""External (custom) ID of the recipient stored in your Address Book."""
    inherited: NotRequired[bool]
    r"""Set to true to fetch recipient data by external ID from inherited main account’s Address Book."""
    custom_id: NotRequired[Nullable[str]]
    r"""Custom shipment ID assigned by the user."""
    postscript: NotRequired[Nullable[str]]
    r"""Optional postscript printed above the recipient data on the envelope."""


class RecipientFromAddressBookByExternalID(BaseModel):
    r"""Select shipment recipient data from the Address Book by external (custom) ID."""

    ext_id: str
    r"""External (custom) ID of the recipient stored in your Address Book."""

    inherited: Optional[bool] = False
    r"""Set to true to fetch recipient data by external ID from inherited main account’s Address Book."""

    custom_id: OptionalNullable[str] = None
    r"""Custom shipment ID assigned by the user."""

    postscript: OptionalNullable[str] = UNSET
    r"""Optional postscript printed above the recipient data on the envelope."""

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

        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
