{% macro construct(property, source) %}
{% set inner_property = property.inner_property %}
{% import "property_templates/" + inner_property.template as inner_template %}
{% if inner_template.construct %}
{% set inner_source = inner_property.python_name + "_data" %}
{{ property.python_name }} = []
_{{ property.python_name }} = {{ source }}
{% if property.required %}
for {{ inner_source }} in (_{{ property.python_name }}):
{% else %}
for {{ inner_source }} in (_{{ property.python_name }} or []):
{% endif %}
    {{ inner_template.construct(inner_property, inner_source) | indent(4) }}
    {{ property.python_name }}.append({{ inner_property.python_name }})
{% else %}
{{ property.python_name }} = cast({{ property.get_type_string(no_optional=True) }}, {{ source }})
{% endif %}
{% endmacro %}

{% macro _transform(property, source, destination, multipart, transform_method) %}
{% set inner_property = property.inner_property %}
{% if multipart %}
{% set multipart_destination = destination %}
{% set destination = "_temp_" + destination %}
{% endif %}
{% import "property_templates/" + inner_property.template as inner_template %}
{% if inner_template.transform %}
{% set inner_source = inner_property.python_name + "_data" %}
{{ destination }} = []
for {{ inner_source }} in {{ source }}:
    {{ inner_template.transform(inner_property, inner_source, inner_property.python_name, transform_method) | indent(4) }}
    {{ destination }}.append({{ inner_property.python_name }})
{% else %}
{{ destination }} = {{ source }}
{% endif %}
{% if multipart %}
{{ multipart_destination }} = (None, json.dumps({{ destination }}).encode(), 'application/json')
{% endif %}
{% endmacro %}

{% macro check_type_for_construct(property, source) %}isinstance({{ source }}, list){% endmacro %}

{% macro transform(property, source, destination, declare_type=True) %}
{% set inner_property = property.inner_property %}
{% set type_string = property.get_type_string(json=True) %}
{% if property.required %}
{{ _transform(property, source, destination, False, "to_dict") }}
{% else %}
{{ destination }}{% if declare_type %}: {{ type_string }}{% endif %} = UNSET
if not isinstance({{ source }}, Unset):
    {{ _transform(property, source, destination, False, "to_dict") | indent(4)}}
{% endif %}
{% endmacro %}

{% macro transform_multipart(property, source, destination) %}
{% set inner_property = property.inner_property %}
{% set type_string = "Union[Unset, tuple[None, bytes, str]]" %}
{% if property.required %}
{{ _transform(property, source, destination, True, "to_dict") }}
{% else %}
{{ destination }}: {{ type_string }} = UNSET
if not isinstance({{ source }}, Unset):
    {{ _transform(property, source, destination, True, "to_dict") | indent(4)}}
{% endif %}
{% endmacro %}

{% macro transform_multipart_body(property, source, destination) %}
{% set inner_property = property.inner_property %}
{% set type_string = property.get_type_string(json=True) %}
{% if property.required %}
{{ _transform(property, source, destination, False, "to_multipart") }}
{% else %}
{{ destination }}: {{ type_string }} = UNSET
if not isinstance({{ source }}, Unset):
    {{ _transform(property, source, destination, False, "to_multipart") | indent(4)}}
{% endif %}
{% endmacro %}
