{% set sidebar_nav = get_sidebar_navigation(request) %}
{% set nav_groups = sidebar_nav.groups if sidebar_nav else [] %}
{% set current_path = request.url.path if request else '' %}

<nav
  class="sidebar-nav"
  aria-label="Primary navigation"
  data-navigation-source="{{ 'server' if sidebar_nav else 'fallback' }}"
>
  <div class="sidebar-nav-controls">
    <button id="nav-prev" class="nav-chevron nav-chevron-prev" aria-label="Previous page">ᐊ</button>
    <button id="nav-next" class="nav-chevron nav-chevron-next" aria-label="Next page">ᐅ</button>
  </div>
  {% if nav_groups %}
    {% for group in nav_groups %}
      {% if group.links %}
        <div class="sidebar-section" data-group-id="{{ group.id }}">
          <div class="sidebar-section-label">{{ group.label }}</div>
          <div class="sidebar-section-links">
            {% for link in group.links %}
              {% set href = link.href %}
              {% if href %}
                {% set match_prefix = link.match_prefix or href %}
                {% if match_prefix == '/' %}
                  {% set is_active = current_path == '/' %}
                {% else %}
                  {% set is_active = current_path.startswith(match_prefix) %}
                {% endif %}
                <div class="sidebar-link-block" data-link-id="{{ link.id }}">
                  <a
                    href="{{ href }}"
                    class="sidebar-link{% if is_active %} is-active{% endif %}{% if link.is_external %} is-external{% endif %}"
                    data-match-prefix="{{ match_prefix }}"
                    {% if link.tooltip %}title="{{ link.tooltip }}"{% endif %}
                    {% if link.is_external %}target="_blank" rel="noopener noreferrer"{% endif %}
                  >
                    {{ link.label }}
                  </a>
                  {% if link.children %}
                    <div class="sidebar-sublinks">
                      {% for child in link.children %}
                        {% set child_href = child.href %}
                        {% if child_href %}
                          {% set child_prefix = child.match_prefix or child_href %}
                          {% if child_prefix == '/' %}
                            {% set child_active = current_path == '/' %}
                          {% else %}
                            {% set child_active = current_path.startswith(child_prefix) %}
                          {% endif %}
                          <a
                            href="{{ child_href }}"
                            class="sidebar-sublink{% if child_active %} is-active{% endif %}{% if child.is_external %} is-external{% endif %}"
                            data-match-prefix="{{ child_prefix }}"
                            {% if child.tooltip %}title="{{ child.tooltip }}"{% endif %}
                            {% if child.is_external %}target="_blank" rel="noopener noreferrer"{% endif %}
                          >
                            {{ child.label }}
                          </a>
                        {% endif %}
                      {% endfor %}
                    </div>
                  {% endif %}
                </div>
              {% endif %}
            {% endfor %}
          </div>
        </div>
      {% endif %}
    {% endfor %}
  {% else %}
    <div class="sidebar-section sidebar-empty-state">
      <div class="sidebar-section-label">Navigation</div>
      <div class="sidebar-section-links">
        <p class="sidebar-empty-copy">Navigation is unavailable.</p>
      </div>
    </div>
  {% endif %}
</nav>
