{#
Documentation Navigation Section Component (Recursive)

Renders an individual section with pages and subsections in the docs nav tree.

Variables:
- section: Section object to render (required)
- section.title: Section title
- section.url: Section URL
- section.index_page: Section index page (optional)
- section.sorted_pages: Pages in section (pre-sorted by weight)
- section.sorted_subsections: Subsections (pre-sorted by weight)
- depth: Current nesting depth for styling (required)
- page: Current page object (for active highlighting)

Features:
- Collapsible section with toggle button
- Clickable section header (links to index page)
- Active page highlighting
- Recursive rendering of subsections
- Smart title display (extracts short names from dotted titles)

Note:
This component calls itself recursively for subsections.
Included by 'partials/docs-nav.html'.

Usage:
{% set depth = 0 %}
{% include 'partials/docs-nav-section.html' %}
#}

<div class="docs-nav-group" data-depth="{{ depth }}">
  {# Section Toggle Button - with link functionality #}
  <div class="docs-nav-group-toggle-wrapper">
    <button class="docs-nav-group-toggle" aria-expanded="false" aria-controls="nav-section-{{ section.url | slugify }}">
      <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
        <polyline points="9 18 15 12 9 6"></polyline>
      </svg>
    </button>
    {# Normalize current page URL for exact matching (remove trailing slash) #}
    {# Use relative_url for comparisons (without baseurl) #}
    {% set current_url = page.relative_url.rstrip('/') if (page is defined and page and page.relative_url) else '' %}

    {% if section.index_page %}
    {% set section_url_normalized = section.index_page.relative_url.rstrip('/') if section.index_page.relative_url else '' %}
    {% set section_title = section.title if section.title and section.title.strip() else (section.slug.replace('-', ' ').replace('_', ' ').title() if section.slug else 'Section') %}
    <a href="{{ section.index_page.url }}"
      class="docs-nav-group-link {% if (page is defined and page and current_url == section_url_normalized) %}active{% endif %}"
      {% if (page is defined and page and current_url == section_url_normalized) %}aria-current="page"{% endif %}>
      {{ section_title }}
    </a>
    {% else %}
    {% set section_title = section.title if section.title and section.title.strip() else (section.slug.replace('-', ' ').replace('_', ' ').title() if section.slug else 'Section') %}
    <span class="docs-nav-group-link">{{ section_title }}</span>
    {% endif %}
  </div>

  {# Section Items #}
  <div class="docs-nav-group-items" id="nav-section-{{ section.url | slugify }}">
    {# Cache sorted lists for O(1) access - computed once per section #}
    {% set section_sorted_pages = section.sorted_pages %}
    {% set section_sorted_subsections = section.sorted_subsections %}
    {# Use relative_url for comparisons (without baseurl) #}
    {% set section_index_url = section.index_page.relative_url if section.index_page else none %}

    {# Regular pages in this section (excluding index page) - sorted by weight #}
    {% for p in section_sorted_pages %}
    {# Use relative_url for comparisons (without baseurl) #}
    {% if p.relative_url != section_index_url and p.title %}
    {# Extract short name for nested items (e.g., "admonitions" from "directives.admonitions") #}
    {% set display_title = p.title.split('.')[-1] if ('.' in p.title) else p.title %}
    {% set p_url_normalized = p.relative_url.rstrip('/') if p.relative_url else '' %}
    <a href="{{ p.url }}"
      class="docs-nav-link {% if (page is defined and page and current_url == p_url_normalized) %}active{% endif %}"
      {% if (page is defined and page and current_url == p_url_normalized) %}aria-current="page"{% endif %}>
      {{ display_title }}
    </a>
    {% endif %}
    {% endfor %}

    {# Subsections (recursive) - sorted by weight #}
    {% for subsection in section_sorted_subsections %}
    {% set section = subsection %}
    {% set depth = depth + 1 %}
    {% include 'partials/docs-nav-section.html' %}
    {% endfor %}
  </div>
</div>
