"""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_extensions import NotRequired, TypedDict


class SenderTypedDict(TypedDict):
    r"""Sender data for the shipment."""

    name: Nullable[str]
    r"""Sender name (person or company) — line 1."""
    address: Nullable[str]
    r"""Street address of the sender."""
    post_code: Nullable[str]
    r"""Postal code. For `PL`, 5-digit values are normalized to the `NN-NNN` format."""
    city: Nullable[str]
    r"""City of the sender."""
    name2: NotRequired[Nullable[str]]
    r"""Sender name (person or company) — line 2."""
    home_number: NotRequired[Nullable[str]]
    r"""Building number of the sender."""
    flat_number: NotRequired[Nullable[str]]
    r"""Apartment (unit) number of the sender."""
    country: NotRequired[Nullable[str]]
    r"""Country code in ISO 3166-1 alpha-2 format. Value is automatically uppercased. Full list: https://www.iso.org/iso-3166-country-codes.html"""


class Sender(BaseModel):
    r"""Sender data for the shipment."""

    name: Nullable[str]
    r"""Sender name (person or company) — line 1."""

    address: Nullable[str]
    r"""Street address of the sender."""

    post_code: Nullable[str]
    r"""Postal code. For `PL`, 5-digit values are normalized to the `NN-NNN` format."""

    city: Nullable[str]
    r"""City of the sender."""

    name2: OptionalNullable[str] = UNSET
    r"""Sender name (person or company) — line 2."""

    home_number: OptionalNullable[str] = UNSET
    r"""Building number of the sender."""

    flat_number: OptionalNullable[str] = UNSET
    r"""Apartment (unit) number of the sender."""

    country: OptionalNullable[str] = UNSET
    r"""Country code in ISO 3166-1 alpha-2 format. Value is automatically uppercased. Full list: https://www.iso.org/iso-3166-country-codes.html"""

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = ["name2", "home_number", "flat_number", "country"]
        nullable_fields = [
            "name",
            "name2",
            "address",
            "home_number",
            "flat_number",
            "post_code",
            "city",
            "country",
        ]
        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
