{#
Bengal OpenAPI Sidebar Navigation
=================================

Smart API navigation grouped by tags.
Shows endpoints organized by tag with method badges.

Context:
- section: Current autodoc section
- sections: All autodoc sections (for navigation)
- current_page: Current page (for active state)
- endpoints: List of endpoint elements (optional, derived from section)
- tags: List of tags with their endpoints (optional)
- api_title: API title for home link (optional)

Kida Features:
- {% with %} for nil-safe optional sections
- {% spaceless %} for compact badge output
- Optional chaining (?.) and null coalescing (??)
#}
{% let title = api_title ?? section?.title ?? 'API Reference' %}
{% let home_href = section?.href ?? '/api/' %}

<nav class="api-sidebar-nav" aria-label="API navigation">
  {# Header with Home Link #}
  <header class="api-sidebar-nav__header">
    <a href="{{ home_href }}" class="api-sidebar-nav__home">
      {{ icon('book-open', size=16) }}
      <span>{{ title }}</span>
    </a>
  </header>

  {# Search (optional, JS-enhanced) #}
  <div class="api-sidebar-nav__search">
    <input
      type="search"
      class="api-sidebar-nav__search-input"
      placeholder="Search endpoints..."
      aria-label="Search API endpoints"
      data-api-search
    >
  </div>

  {# Endpoint Groups by Tag #}
  <div class="api-sidebar-nav__groups">
    {% with section?.subsections as subsections %}
    {% if subsections | length > 0 %}
    {# Navigate by subsections (tag groups) #}
    {% for subsection in subsections %}
    {% let tag_name = subsection?.title ?? 'Endpoints' %}
    {% let tag_pages = subsection?.pages ?? [] %}
    {% let endpoint_count = tag_pages | length %}

    <details class="api-sidebar-nav__group" open>
      <summary class="api-sidebar-nav__group-header">
        <span class="api-sidebar-nav__group-name">{{ tag_name }}</span>
        <span class="api-sidebar-nav__group-count">{{ endpoint_count }}</span>
      </summary>

      <ul class="api-sidebar-nav__endpoints">
        {% for ep_page in tag_pages %}
        {% let ep_meta = ep_page?.metadata ?? {} %}
        {% let ep_method = ep_meta?.method ?? 'GET' %}
        {% let ep_path = ep_meta?.path ?? ep_page?.title ?? '' %}
        {% let is_deprecated = ep_meta?.deprecated ?? false %}
        {% let is_active = current_page?.href == ep_page?.href %}

        <li class="api-sidebar-nav__endpoint{% if is_active %} api-sidebar-nav__endpoint--active{% end %}{% if is_deprecated %} api-sidebar-nav__endpoint--deprecated{% end %}">
          <a href="{{ ep_page?.href ?? '#' }}" class="api-sidebar-nav__link">
            {% spaceless %}
            <span class="api-method api-method--xs api-method--{{ ep_method |> lower }}">{{ ep_method }}</span>
            {% end %}
            <span class="api-sidebar-nav__path">{{ ep_path | truncate(30) }}</span>
            {% if is_deprecated %}
            <span class="api-sidebar-nav__deprecated" title="Deprecated">⚠</span>
            {% end %}
          </a>
        </li>
        {% end %}
      </ul>
    </details>
    {% end %}
    {% else %}
    {# Flat list of pages (no subsections) #}
    {% with section?.pages as pages %}
    {% if pages | length > 0 %}
    <ul class="api-sidebar-nav__endpoints api-sidebar-nav__endpoints--flat">
      {% for ep_page in pages %}
      {% if ep_page != section?.index_page %}
      {% let ep_meta = ep_page?.metadata ?? {} %}
      {% let ep_method = ep_meta?.method ?? 'GET' %}
      {% let ep_path = ep_meta?.path ?? ep_page?.title ?? '' %}
      {% let is_deprecated = ep_meta?.deprecated ?? false %}
      {% let is_active = current_page?.href == ep_page?.href %}

      <li class="api-sidebar-nav__endpoint{% if is_active %} api-sidebar-nav__endpoint--active{% end %}{% if is_deprecated %} api-sidebar-nav__endpoint--deprecated{% end %}">
        <a href="{{ ep_page?.href ?? '#' }}" class="api-sidebar-nav__link">
          {% spaceless %}
          <span class="api-method api-method--xs api-method--{{ ep_method |> lower }}">{{ ep_method }}</span>
          {% end %}
          <span class="api-sidebar-nav__path">{{ ep_path | truncate(35) }}</span>
          {% if is_deprecated %}
          <span class="api-sidebar-nav__deprecated" title="Deprecated">⚠</span>
          {% end %}
        </a>
      </li>
      {% end %}
      {% end %}
    </ul>
    {% end %}
    {% end %}
    {% end %}
    {% end %}
  </div>
</nav>
