{% macro wrap_string(string, wrapstring, width=95) %}
{%- set normalized_string = string -%}
{# Normalize multi-line descriptions by collapsing internal newlines to spaces, but preserve special blocks #}
{# Skip normalization for pre-formatted content (strings starting with whitespace like code examples) #}
{%- if '\n* ' not in string and string == string.lstrip() -%}
    {%- if '.. code-block::' in string -%}
        {# Split at code block and only normalize the prose before it #}
        {%- set parts = string.split('.. code-block::') -%}
        {%- set prose = parts[0].rstrip() -%}
        {%- set code_block = '.. code-block::' + parts[1:] | join('.. code-block::') -%}
        {%- if '\n\n' not in prose -%}
            {%- set normalized_lines = [] -%}
            {%- for line in prose.split('\n') -%}
                {%- set stripped = line.strip() -%}
                {%- if stripped -%}
                    {%- set _ = normalized_lines.append(stripped) -%}
                {%- endif -%}
            {%- endfor -%}
            {%- set normalized_string = normalized_lines | join(' ') + '\n\n' + code_block -%}
        {%- endif -%}
    {%- elif '\n\n' not in string -%}
        {%- set normalized_lines = [] -%}
        {%- for line in string.split('\n') -%}
            {%- set stripped = line.strip() -%}
            {%- if stripped -%}
                {%- set _ = normalized_lines.append(stripped) -%}
            {%- endif -%}
        {%- endfor -%}
        {%- set normalized_string = normalized_lines | join(' ') -%}
    {%- endif -%}
{%- endif -%}
{{ normalized_string | replace("\\", "\\\\") | wordwrap(width=width, break_long_words=False, break_on_hyphens=False, wrapstring=wrapstring)}}{% endmacro %}

{% macro description(builder, serializer) %}
{% set example_template = serializer.example_template(builder) %}
{% set param_description_and_response_docstring = serializer.param_description_and_response_docstring(builder) %}
{% set ns = namespace(line_too_long=false) %}
{% for item in param_description_and_response_docstring %}
    {% if item and serializer.line_too_long(wrap_string(item, wrapstring='\n ').split('\n'), 8) %}
        {% set ns.line_too_long = true %}
    {% endif %}
{% endfor %}
    {% for description in serializer.description_and_summary(builder) %}
        {% if description %}
{% set description = wrap_string(description, wrapstring='\n') %}
{{ '"""' + description if loop.first else description }}
        {% else %}

        {% endif %}
    {% endfor %}
    {% for description in param_description_and_response_docstring %}
        {% if description %}
{% set description = wrap_string(description, wrapstring='\n ') %}
{{ " " if description[0] != ":" and description[0] != " " else "" }}{{ description}}
        {% else %}

        {% endif %}
{% endfor %}
{% if example_template %}

Example:
    .. code-block:: python
    {% for template_line in example_template %}
        {% if template_line %}
            {% set wrap_amount = (template_line | length) - (template_line.lstrip() | length) + 10 %}
        {{  wrap_string(template_line, wrapstring='\n' + " " * wrap_amount, width=(95 - wrap_amount)) }}
        {% else %}

        {% endif %}
    {% endfor %}
{% endif %}
"""
{% endmacro %}

{% macro serialize(lines) %}
{% for line in lines %}
    {% if line %}
{{ line }}
    {% else %}

    {% endif %}
{% endfor %}{% endmacro %}

{% macro serialize_with_wrap(lines, wrapstring) %}
{% for line in lines %}
    {% if line %}
{{ wrap_string(line, wrapstring=wrapstring) }}
    {% else %}

    {% endif %}
{% endfor %}{% endmacro %}

{% macro declare_serializer(code_model) %}
{% if code_model.has_non_abstract_operations %}
_SERIALIZER = Serializer()
    {% if code_model.need_utils_serialization %}
_SERIALIZER.client_side_validation = False
    {% endif %}
{% endif %}
{% endmacro %}

{% macro generate_overloads(operation_serializer, operation) %}
{% for overload in operation.overloads %}
{{ operation_serializer.method_signature_and_response_type_annotation(overload) }}
{% if not operation.internal %}
    {{ description(overload, operation_serializer) | indent }}
{% else %}
    ...
{% endif %}
{% endfor %}{% endmacro %}
