{#
    This is a custom template derived from:
    https://github.com/openapi-generators/openapi-python-client/blob/v0.22.0/openapi_python_client/templates/types.py.jinja
#}
""" Contains some shared types for properties """

from collections.abc import MutableMapping
from http import HTTPStatus
{# CHANGE START (1) - add necessary imports for check_nested_instance function  #}
from typing import BinaryIO, Generic, Optional, TypeVar, Literal, get_args, get_origin
from . import models
{# CHANGE END (1) #}

from attrs import define


class Unset:
    def __bool__(self) -> Literal[False]:
        return False


UNSET: Unset = Unset()

{# Used as `FileProperty._json_type_string` #}
FileJsonType = tuple[Optional[str], BinaryIO, Optional[str]]


@define
class File:
    """ Contains information for file uploads """

    payload: BinaryIO
    file_name: Optional[str] = None
    mime_type: Optional[str] = None

    def to_tuple(self) -> FileJsonType:
        """ Return a tuple representation that httpx will accept for multipart/form-data """
        return self.file_name, self.payload, self.mime_type


T = TypeVar("T")


@define
class Response(Generic[T]):
    """ A response from an endpoint """

    status_code: HTTPStatus
    content: bytes
    headers: MutableMapping[str, str]
    parsed: Optional[T]

{# CHANGE START (2) - add base class for OSIDB models #}
class OSIDBModel:
    """ Base class for all 'non-primitive' and 'non-enum' models  """
    pass
{# CHANGE END (2) #}

{# CHANGE START (3) - add function to check nested instances types #}
def check_nested_instance(body, expected_type):
    """ Helper function to check nested list instances"""
    if get_origin(expected_type) is list:
        inner_expected_type_name = get_args(expected_type)[0]
        inner_expected_type = getattr(models, inner_expected_type_name)
        return all(check_nested_instance(item, inner_expected_type) for item in body)
    else:
        return isinstance(body, expected_type)
{# CHANGE END (3) #}

__all__ = ["UNSET", "File", "FileJsonType", "OSIDBModel", "Response", "Unset", "check_nested_instance"]
