{#
================================================================================
Track Sidebar Component (Kida-Native v2)
================================================================================

Left sidebar for track pages showing:
- Current track section navigation
- Other available tracks

Data Attributes (for JS):
- data-bengal="track-nav": Main navigation container
- data-track-id="slug": Track identifier for progress persistence
- data-track-section="N": Section link (N = section number)
- data-track-target="#id": Target section ID
- data-track-visited: Set by JS when section has been viewed
- data-track-completed: Set by JS when section scrolled past
- aria-current="step": Set by JS on active section

KIDA FEATURES USED:
- {% let %} for scoped variable binding
- {% with %} for nil-resilient data access
- {% match %} for clean conditional rendering
- Optional chaining (?.) and null coalescing (??)
- Pipeline operators (|>) for filter chains
- {% spaceless %} for clean navigation HTML

Usage:
{% include 'partials/track-sidebar.html' %}
================================================================================
#}

{% let track_id = params?.track_id ?? page?.slug ?? '' %}

{# Nil-resilient track lookup with {% with %} #}
{% with site?.data?.tracks as all_tracks %}
{% let current_track = all_tracks[track_id] if (track_id and all_tracks and track_id in all_tracks) else none %}

<nav class="track-sidebar" aria-label="Track navigation" data-bengal="track-nav" data-track-id="{{ track_id }}">
  {# =========================================================================
     CURRENT TRACK SECTIONS (Primary navigation)
     Uses track_item_pages from parent context if available (pre-computed)
     ========================================================================= #}
  {% with current_track?.items as track_items %}
  {% if track_items | length > 0 %}
  <section class="track-nav-sections">
    <h3 class="track-nav-title">{{ current_track?.title ?? t('tracks.sections', default='Sections') }}</h3>
    {% spaceless %}
    <ol class="track-nav-list">
      {% for item_slug in track_items %}
      {# Use pre-computed page from parent context, fallback to get_page() #}
      {% let item_page = track_item_pages[item_slug] if (track_item_pages is defined and item_slug in track_item_pages) else get_page(item_slug) %}
      {% if item_page %}
      <li>
        <a href="#track-section-{{ loop.index }}"
           class="track-nav-link"
           data-track-section="{{ loop.index }}"
           data-track-target="#track-section-{{ loop.index }}">
          <span class="track-nav-number">{{ loop.index }}</span>
          <span class="track-nav-label">{{ item_page?.title ?? item_slug }}</span>
        </a>
      </li>
      {% end %}
      {% end %}
    </ol>
    {% end %}
  </section>
  {% end %}
  {% end %}

  {# =========================================================================
     OTHER TRACKS SECTION
     Pre-compute track_id → page mapping ONCE (O(n) instead of O(n×m))
     ========================================================================= #}
  {% let track_count = (all_tracks ?? {}) | length %}
  {% if track_count > 1 %}
  {# Build track_id → page map once #}
  {% let all_track_pages = {} %}
  {% for p in (site?.pages ?? []) %}
  {% let tid = p?.metadata?.track_id ?? '' %}
  {% if tid and tid != track_id %}
  {% let all_track_pages = all_track_pages | merge({tid: p}) %}
  {% end %}
  {% end %}

  <section class="track-nav-other">
    <h3 class="track-nav-title">{{ t('tracks.other', default='Other Tracks') }}</h3>
    <ul class="track-nav-tracks">
      {% for id, track in all_tracks |> items %}
      {% if id != track_id %}
      {# O(1) lookup from pre-computed map #}
      {% let track_page = all_track_pages[id] if id in all_track_pages else none %}
      {% if track_page %}
      <li>
        <a href="{{ track_page.href }}" class="track-nav-track-link">
          <span class="track-nav-track-title">{{ track?.title ?? id }}</span>
          <span class="track-nav-track-meta">
            {% let lesson_count = (track?.items ?? []) | length %}
            {% match lesson_count %}
            {% case 1 %}1 lesson
            {% case n %}{{ n }} lessons
            {% end %}
          </span>
        </a>
      </li>
      {% end %}
      {% end %}
      {% end %}
    </ul>
  </section>
  {% end %}
</nav>
{% end %}
