"""Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT."""
# @generated-id: b9c1fba62eb4

from __future__ import annotations
from .sales_invoice_payment_details_source_response import (
    SalesInvoicePaymentDetailsSourceResponse,
)
from mollie import models
from mollie.types import BaseModel, Nullable, OptionalNullable, UNSET, UNSET_SENTINEL
import pydantic
from pydantic import field_serializer, model_serializer
from typing_extensions import Annotated, NotRequired, TypedDict


class SalesInvoicePaymentDetailsResponseTypedDict(TypedDict):
    source: SalesInvoicePaymentDetailsSourceResponse
    r"""The way through which the invoice is to be set to paid."""
    source_reference: NotRequired[Nullable[str]]
    r"""A reference to the payment the sales invoice is paid by. Required for `source` values `payment-link` and
    `payment`.
    """


class SalesInvoicePaymentDetailsResponse(BaseModel):
    source: SalesInvoicePaymentDetailsSourceResponse
    r"""The way through which the invoice is to be set to paid."""

    source_reference: Annotated[
        OptionalNullable[str], pydantic.Field(alias="sourceReference")
    ] = UNSET
    r"""A reference to the payment the sales invoice is paid by. Required for `source` values `payment-link` and
    `payment`.
    """

    @field_serializer("source")
    def serialize_source(self, value):
        if isinstance(value, str):
            try:
                return models.SalesInvoicePaymentDetailsSourceResponse(value)
            except ValueError:
                return value
        return value

    @model_serializer(mode="wrap")
    def serialize_model(self, handler):
        optional_fields = set(["sourceReference"])
        nullable_fields = set(["sourceReference"])
        serialized = handler(self)
        m = {}

        for n, f in type(self).model_fields.items():
            k = f.alias or n
            val = serialized.get(k, serialized.get(n))
            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


try:
    SalesInvoicePaymentDetailsResponse.model_rebuild()
except NameError:
    pass
