{% from "property_macros.py.jinja" import get_type_string %}
{% from "property_templates/helpers.jinja" import guarded_statement %}

{%- macro get_default_string(prop) -%}
{%- if prop.default is not none -%} = {{ prop.default }}
{%- elif not prop.required or prop.nullable -%} = None
{%- endif -%}
{%- endmacro -%}

{% macro property_to_string(prop) %}
{{ prop.python_name }}: {{ get_type_string(prop) }} {{ get_default_string(prop) }}
{% endmacro %}

{% macro header_params(endpoint) %}
{% if endpoint.header_parameters %}
    {% for parameter in endpoint.header_parameters.values() %}
        {% set destination = 'headers["' +  parameter.name + '"]' %}
        {% import "property_templates/" + parameter.template as param_template %}
        {% if param_template.transform_header %}
            {% set statement = param_template.transform_header(parameter, parameter.python_name, destination) %}
        {% else %}
            {% set statement = destination + " = " + parameter.python_name %}
        {% endif %}
{{ guarded_statement(parameter, parameter.python_name, statement) }}
    {% endfor %}
{% endif %}
{% endmacro %}

{% macro cookie_params(endpoint) %}
{% if endpoint.cookie_parameters %}
    {% for parameter in endpoint.cookie_parameters.values() %}
        {% if parameter.required %}
cookies["{{ parameter.name}}"] = {{ parameter.python_name }}
        {% endif %}
    {% endfor %}
{% endif %}
{% endmacro %}


{% macro query_params(endpoint) %}
{% if endpoint.query_parameters %}
params: Dict[str, Any] = {}
{% for property in endpoint.query_parameters.values() %}
    {% set destination = property.python_name %}
    {% import "property_templates/" + property.template as prop_template %}
    {% if prop_template.transform %}
        {% set destination = "json_" + property.python_name %}
{{ prop_template.transform(property, property.python_name, destination) }}
    {% endif %}
    {%- if not property.json_is_dict %}
params["{{ property.name }}"] = {{ destination }}
    {% else %}
{{ guarded_statement(property, destination, "params.update(" + destination + ")") }}
    {% endif %}
{% endfor %}


params = {k: v for k, v in params.items() if v is not None}
{% endif %}
{% endmacro %}

{% macro json_body(endpoint) %}
{% if endpoint.json_body %}
    {% set property = endpoint.json_body %}
    {% set destination = "json_" + property.python_name %}
    {% import "property_templates/" + property.template as prop_template %}
    {% if prop_template.transform %}
{{ prop_template.transform(property, property.python_name, destination) }}
    {% else %}
{{ destination }} = {{ property.python_name }}
    {% endif %}
{% endif %}
{% endmacro %}

{% macro multipart_body(endpoint) %}
{% if endpoint.multipart_body %}
    {% set property = endpoint.multipart_body %}
    {% set destination = "multipart_" + property.python_name %}
    {% import "property_templates/" + property.template as prop_template %}
    {% if prop_template.transform_multipart %}
{{ prop_template.transform_multipart(property, property.python_name, destination) }}
    {% endif %}
{% endif %}
{% endmacro %}

{# The all the kwargs passed into an endpoint (and variants thereof)) #}
{% macro arguments(endpoint) %}
{# Client #}
client: Client,
{# path parameters #}
{% for parameter in endpoint.path_parameters.values() %}
{{ property_to_string(parameter) }},
{% endfor %}
{# Form data if any #}
{% if endpoint.form_body_class %}
form_data: {{ endpoint.form_body_class.name }},
{% endif %}
{# Multipart data if any #}
{% if endpoint.multipart_body %}
multipart_data: {{ endpoint.multipart_body.get_type_string() }},
{% endif %}
{# JSON body if any #}
{% if endpoint.json_body %}
json_body: {{ endpoint.json_body.get_type_string() }},
{% endif %}
{# query parameters #}
{% for parameter in endpoint.query_parameters.values() %}
{{ property_to_string(parameter) }},
{% endfor %}
{% for parameter in endpoint.header_parameters.values() %}
{{ property_to_string(parameter) }},
{% endfor %}
{# cookie parameters #}
{% for parameter in endpoint.cookie_parameters.values() %}
{{ property_to_string(parameter) }},
{% endfor %}
{% endmacro %}
