{% import _self as list %}
{% apply spaceless %}

{#
  Parameters:
    - "items" (array) (default: []): format: [
        {
          "label" (string)
          "nested" (optional) (object of type Ordered List)
        },
        ...
      ]
    - "variant" (string) (default: '') List variant; can be 'no-marker', 'divider'
    - "extra_classes" (string) (default: '') Extra classes (space separated)
    - "extra_attributes" (array) (default: []): format: [
        {
          "name" (string) Attribute name, eg. 'data-test'
          "value" (string) Attribute value, eg: 'data-test-1'
        }, ...
      ]
#}

{# Define main macro #}
{% macro ordered_list(items, extra_classes, extra_attributes, variant) %}{% apply spaceless %}
  {% import _self as list %}

  {# Internal properties #}
  {% set _items = items|default([]) %}
  {% set _css_class = 'ecl-ordered-list' %}
  {% set _variant = variant|default('') %}
  {% set _extra_attributes = '' %}

  {# Internal logic - Process properties #}

  {% if _variant is not empty %}
    {% set _css_class = _css_class ~ ' ' ~ _css_class ~ '--' ~ _variant %}
  {% endif %}

  {% if extra_classes is defined and extra_classes is not empty %}
    {% set _css_class = _css_class ~ ' ' ~ extra_classes %}
  {% endif %}

  {% if extra_attributes is defined and extra_attributes is not empty and extra_attributes is iterable %}
    {% for attr in extra_attributes %}
      {% if attr.value is defined %}
        {% set _extra_attributes = _extra_attributes ~ ' ' ~ attr.name ~ '="' ~ attr.value|e('html_attr') ~ '"' %}
      {% else %}
        {% set _extra_attributes = _extra_attributes ~ ' ' ~ attr.name %}
      {% endif %}
    {% endfor %}
  {% endif %}

  {# Print the result #}
  {% if _items is not empty %}
    <ol
      class="{{ _css_class }}"
      {{ _extra_attributes|raw }}
    >
      {% for _item in _items %}
        {% set _item_class = 'ecl-ordered-list__item' %}

        <li class="{{ _item_class }}">
          {{- _item.label -}}
          {% if _item.nested is not empty and _item.nested is iterable %}
            {{- list.ordered_list(_item.nested, '', [], '') -}}
          {% endif -%}
        </li>
      {% endfor %}
    </ol>
  {% endif %}

{% endapply %}{% endmacro %}

{# Render macro #}
{{ list.ordered_list(items, extra_classes, extra_attributes, variant) }}

{% endapply %}
