{#
Documentation Navigation Component

Displays full hierarchical sidebar navigation for documentation sites.

Variables:
- page: Current page object (required)
- page._section: Current section (for determining root)
- page._section.root: Root section to display from
- site: Site object (fallback when no section)

Features:
- Hierarchical tree structure
- Active page highlighting
- Active trail marking
- Scoped to current section tree
- Recursive subsection rendering
- Keyboard accessible

Note:
Includes 'partials/docs-nav-section.html' for recursive rendering.
If page has a section, displays only that section's tree.
If no section, falls back to showing all top-level sections.

Usage:
{% include 'partials/docs-nav.html' %}
#}

<nav class="docs-nav" aria-label="Documentation pages">
  {# Navigation Tree #}
  <div class="docs-nav-tree">
    {# Determine the root section to display #}
    {# Strategy: If current page has a section, find its root ancestor #}
    {# This allows the nav to be scoped to just the docs tree, not the entire site #}
    {# Get the root section to display the navigation tree from the top #}
    {% set root_section = page._section.root if (page is defined and page and page._section) else none %}

    {# If we have a root section, show only its tree #}
    {% if root_section %}
    {# Cache sorted lists and subsection index URLs for O(1) lookups #}
    {% set sorted_pages = root_section.sorted_pages %}
    {% set sorted_subsections = root_section.sorted_subsections %}
    {# subsection_index_urls is a set from Python for O(1) membership checks #}
    {% set subsection_index_urls = root_section.subsection_index_urls %}
    {% set root_index_url = root_section.index_page.relative_url if root_section.index_page else none %}

    {# 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 '' %}

    {# Show the section's index page if it exists #}
    {% if root_section.index_page %}
    {% set root_url_normalized = root_index_url.rstrip('/') if root_index_url else '' %}
    {% set root_title = root_section.index_page.title if root_section.index_page.title and root_section.index_page.title.strip() else (root_section.slug.replace('-', ' ').replace('_', ' ').title() if root_section.slug else 'API Reference') %}
    <a href="{{ root_section.index_page.url }}"
      class="docs-nav-link {% if (page is defined and page and current_url == root_url_normalized) %}active{% endif %}"
      {% if (page is defined and page and current_url == root_url_normalized) %}aria-current="page"{% endif %}>
      {{ root_title }}
    </a>
    {% endif %}

    {# Show regular pages (O(n) with O(1) lookups) - sorted by weight #}
    {% for p in sorted_pages %}
    {# Skip root index and subsection index pages - now O(1) set lookup! #}
    {# Use relative_url for comparisons (without baseurl) #}
    {% if p.relative_url != root_index_url and p.relative_url not in subsection_index_urls %}
    {% 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 %}>
      {{ p.title }}
    </a>
    {% endif %}
    {% endfor %}

    {# Show subsections - sorted by weight #}
    {% for section in sorted_subsections %}
    {% set depth = 0 %}
    {% include 'partials/docs-nav-section.html' %}
    {% endfor %}
    {% else %}
    {# Fallback: Show all top-level sections (original behavior) #}
    {% set top_sections = site.sections | selectattr('parent', 'none') | list %}

    {# 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 '' %}

    {# Show any top-level pages first (regular pages without a section) #}
    {% set top_pages = site.pages | selectattr('_section', 'none') | rejectattr('metadata._generated', 'defined') | list
    %}
    {% for p in top_pages %}
    {# Use relative_url for comparisons (without baseurl) #}
    {% 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 %}>
      {{ p.title }}
    </a>
    {% endfor %}

    {# Show all top-level sections #}
    {% for section in top_sections %}
    {% set depth = 0 %}
    {% include 'partials/docs-nav-section.html' %}
    {% endfor %}
    {% endif %}
  </div>
</nav>
