{% from "/schema_templates/json/macro_restriction.jinja2" import restriction with context %}
{% from "/schema_templates/schema_macros.jinja2" import badge %}

{% macro tabbed_section(operator, current_node) %}
    {% include "/schema_templates/json/tabbed_section.jinja2" %}
{% endmacro %}

{% macro content(schema, skip_headers=False) %}
    {% set keys = schema.keywords %}

    {# Resolve type #}
    {% set type_name = schema.type_name %}

    {% if not skip_headers %}
        {% if config.show_breadcrumbs %}
            {% include "/schema_templates/json/breadcrumbs.jinja2" %}
        {% endif %}

        {# Display type #}
        {% if not schema is combining %}
            <div class="row">
                <div class="col">
                    {{ badge("secondary", "Type: " + type_name) }}
                </div>
            </div>
        {% endif %}

        {# Display default #}
        {% set default_value = schema.default_value %}
        {% if default_value %}
            <div class="row">
                <div class="col">
                    {{ badge("secondary", "Default: " + default_value) }}
                </div>
            </div>
        {% endif %}
        <br/>

        {% set description = (schema | get_description) %}
        {% include "/schema_templates/json/section_description.jinja2" %}
    {% endif %}


    {% if schema.should_be_a_link(config) %}
        <a href="#{{ schema.links_to.html_id }}" onclick="anchorLink('{{ schema.links_to.html_id }}')" class="ref-link">Same definition as {{ schema.links_to.link_name }}</a>
    {% elif schema.refers_to %}
        {{ content(schema.refers_to_merged, True) }}
    {% else %}
        {# Handle having oneOf or allOf with only one condition #}
        {% if schema.kw_all_of and (schema.kw_all_of.array_items | length) == 1 %}
            {{ content(schema.kw_all_of.array_items[0]) }}
        {% elif schema.kw_any_of and (schema.kw_any_of.array_items | length) == 1 %}
            {{ content(schema.kw_any_of.array_items[0]) }}
        {% else %}
            {% if schema.explicit_no_additional_properties %}
                {{ badge("info", "No Additional Properties") }}
            {% endif %}

            {# Combining: allOf, anyOf, oneOf, not #}
            {% if schema.kw_all_of %}
                <div class="all-of-value" id="{{ schema.kw_all_of.html_id }}">{{ tabbed_section("allOf", schema.kw_all_of) }}</div>
            {% endif %}
            {% if schema.kw_any_of %}
                <div class="any-of-value" id="{{ schema.kw_any_of.html_id }}">{{ tabbed_section("anyOf", schema.kw_any_of) }}</div>
            {% endif %}
            {% if schema.kw_one_of %}
                <div class="one-of-value" id="{{ schema.kw_one_of.html_id }}">{{ tabbed_section("oneOf", schema.kw_one_of) }}</div>
            {% endif %}
            {% if schema.kw_not %}
                {% include "/schema_templates/json/section_not.jinja2" %}
            {% endif %}

            {# Enum and const #}
            {% if schema.kw_enum %}
                <div class="enum-value" id="{{ schema.kw_enum.html_id }}">
                <h4>Must be one of:</h4>
                <ul class="list-group">
                {% for enum_choice in schema.kw_enum.array_items %}
                    <li class="list-group-item enum-item">{{ enum_choice.literal | python_to_json }}</li>
                {% endfor %}
                </ul>
                </div>
            {% endif %}
            {% if schema.kw_const %}
                <span class="const-value" id="{{ schema.kw_const.html_id }}">Specific value: <code>{{ schema.kw_const.raw | python_to_json }}</code></span>
            {% endif %}

            {# Pattern (Regular Expression) #}
            {% if schema.kw_pattern %}
                <span class="pattern-value" id="{{ schema.kw_pattern.html_id }}">Must match regular expression: <code>{{ schema.kw_pattern.literal | escape }}</code></span>
            {% endif %}

            {# Conditional subschema, or if-then-else section #}
            {% if schema.has_conditional %}
                {% include "/schema_templates/json/section_conditional_subschema.jinja2" %}
            {% endif %}

            {# Required properties that are not defined under "properties". They will only be listed #}
            {% include "/schema_templates/json/section_undocumented_required_properties.jinja2" %}

            {# Show the requested type(s) #}
            {% include "/schema_templates/json/badge_type.jinja2" %}

            {# Show array restrictions #}
            {% if type_name.startswith("array") %}
                {% include "/schema_templates/json/section_array.jinja2" %}
            {% endif %}

            {# Display examples #}
            {% set examples = schema.examples %}
            {% if examples %}
                {% include "/schema_templates/json/section_examples.jinja2" %}
            {% endif %}

            {# Properties, pattern properties, additional properties #}
            {% for sub_property in schema.iterate_properties %}
                {% include "/schema_templates/json/section_properties.jinja2" %}
            {% endfor %}

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