{% from "macro_restriction.html" import restriction with context %}

{%- macro tabbed_section(operator, current_node) -%}
    {% include "tabbed_section.html" %}
{%- endmacro -%}

{%- macro content(schema, description="") -%}
    {% set keys = schema.keywords %}

    {%- set description = (schema | get_description) if not description else description -%}

    {%- if schema.should_be_a_link(config) -%}
        {# There can be a description next to $ref, so let's display it. Fallback to description of referenced element #}
        {% include "section_description.html" %}

        <a href="#{{ schema.refers_to.html_id }}" onclick="anchorLink('{{ schema.refers_to.html_id }}')">Same definition as {{ schema.refers_to.html_id }}</a>
    {%- elif schema.refers_to -%}
        {{ content(schema.refers_to, description) }}
    {%- else -%}
        {# Get the content of keywords, rejecting those that are under "properties" or "patternProperties" #}
        {%- set kw_all_of = schema.get_keyword("allOf") -%}
        {%- set kw_any_of = schema.get_keyword("anyOf") -%}
        {%- set kw_one_of = schema.get_keyword("oneOf") -%}
        {%- set kw_not = schema.get_keyword("not") -%}
        {%- set kw_if = schema.get_keyword("if") -%}
        {%- set kw_else = schema.get_keyword("else") -%}
        {%- set kw_then = schema.get_keyword("then") -%}
        {%- set kw_enum = schema.get_keyword("enum") -%}
        {%- set kw_const = schema.get_keyword("const") -%}
        {%- set kw_pattern = schema.get_keyword("pattern") -%}
        {%- set kw_properties = schema.get_keyword("properties") -%}
        {%- set kw_pattern_properties = schema.get_keyword("patternProperties") -%}

        {# Handle having oneOf or allOf with only one condition #}
        {%- if kw_all_of and (kw_all_of.array_items | length) == 1 -%}
            {%  include "section_description.html" %}
            {{ content(kw_all_of.array_items[0]) }}
        {%- elif kw_any_of and (kw_any_of.array_items | length) == 1 -%}
            {%  include "section_description.html" %}
            {{ content(kw_any_of.array_items[0]) }}
        {%- else -%}
            {# Resolve type #}
            {%- set type_name = schema | get_type_name -%}

            {# Display type #}
            {%- if not schema is combining -%}
                <span class="badge badge-dark value-type">Type: {{ type_name }}</span>
            {%- endif -%}
            {%-  set default_value = schema | get_default -%}
            {%- if default_value -%}
                {{ " " }}<span class="badge badge-success default-value">Default: {{ default_value }}</span>
            {%- endif -%}

            {%  include "section_description.html" %}

            {# Combining: allOf, anyOf, oneOf, not #}
            {%- if kw_all_of -%}
                {% set current_node = keys["allOf"] %}
                <div class="all-of-value" id="{{ current_node.html_id }}">{{ tabbed_section("allOf", current_node) }}</div>
            {%- endif -%}
            {%- if kw_any_of -%}
                {% set current_node = keys["anyOf"] %}
                <div class="any-of-value" id="{{ current_node.html_id }}">{{ tabbed_section("anyOf", current_node) }}</div>
            {%- endif -%}
            {%- if kw_one_of -%}
                {% set current_node = keys["oneOf"] %}
                <div class="one-of-value" id="{{ current_node.html_id }}">{{ tabbed_section("oneOf", current_node) }}</div>
            {%- endif -%}
            {%- if kw_not -%}
                {% include "section_not.html" %}
            {%- endif -%}

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

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

            {# Conditional subschema, or if-then-else section #}
            {%- if kw_if and (kw_then or kw_else) -%}
                {% include "section_conditional_subschema.html" %}
            {%- endif -%}

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

            {# Show the requested type(s) #}
            {% include "badge_type.html" %}

            {# Show array restrictions #}
            {%- if type_name.startswith("array") -%}
                {% include "section_array.html" %}
            {%- endif -%}

            {# Display examples #}
            {%- set examples = schema.examples -%}
            {%- if examples -%}
                {% include "section_examples.html" %}
            {%- endif -%}

            {%- set required_properties = schema | get_required_properties -%}
            {%- if kw_properties -%}
                {% set is_pattern_property = False %}

                {%- for sub_property_name, sub_property in kw_properties.keywords.items() -%}
                    {% include "section_properties.html" %}
                {%- endfor -%}
            {%- endif -%}

            {%- if kw_pattern_properties -%}
                {% set is_pattern_property = True %}

                {%- for sub_property_name, sub_property in kw_pattern_properties.keywords.items() -%}
                    {% include "section_properties.html" %}
                {%- endfor -%}
            {%- endif -%}

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