{% from "govuk/macros/attributes.njk" import govukAttributes -%}

<nav class="x-govuk-sub-navigation{%- if params.classes %} {{ params.classes }}{% endif %}" aria-labelledby="sub-navigation-heading"
  {{- govukAttributes(params.attributes) }}>
  <h2 class="govuk-visually-hidden" id="sub-navigation-heading">{{ params.visuallyHiddenTitle or "Pages in this section" }}</h2>
{% for theme, items in params.items | groupby("theme") %}
  {% if theme != "undefined" %}
  <h3 class="x-govuk-sub-navigation__theme">
    {{- theme -}}
  </h3>
  {% endif %}
  <ul class="x-govuk-sub-navigation__section">
  {% for item in items %}
    {% if item %}
    <li class="x-govuk-sub-navigation__section-item{% if item.parent or item.current %} x-govuk-sub-navigation__section-item--current{% endif %}">
      <a class="x-govuk-sub-navigation__link" href="{{ item.href }}"{% if item.current %} aria-current="true"{% endif %}>{{ item.text }}</a>
      {% if item.parent and item.children | length > 0 %}
      <ul class="x-govuk-sub-navigation__section x-govuk-sub-navigation__section--nested">
      {% for child in item.children %}
        <li class="x-govuk-sub-navigation__section-item">
          <a class="x-govuk-sub-navigation__link" href="{{ child.href }}"{% if child.current %} aria-current="true"{% endif %}>
            {{- child.text -}}
          </a>
        </li>
      {% endfor %}
      </ul>
      {% endif %}
    </li>
    {% endif %}
  {% endfor %}
  </ul>
{% endfor %}
</nav>
