// nav.js — minimaler Hash-basierter View-Switch fuer die Sidebar.
// Zeigt genau eine .view[data-view] und markiert das aktive Nav-Item.

import { $$ } from "./ui.js";

export function initNav(defaultView = "overview") {
  const items = $$(".nav-item[data-view]:not(.disabled)");
  const views = $$(".view[data-view]");
  const valid = new Set(views.map(v => v.dataset.view));

  function show(view) {
    if (!valid.has(view)) view = defaultView;
    views.forEach(v => (v.hidden = v.dataset.view !== view));
    items.forEach(i => i.classList.toggle("active", i.dataset.view === view));
    if (location.hash !== "#/" + view) location.hash = "#/" + view;
  }

  items.forEach(i =>
    i.addEventListener("click", e => {
      e.preventDefault();
      show(i.dataset.view);
    })
  );
  window.addEventListener("hashchange", () => show(location.hash.replace("#/", "")));

  show(location.hash.replace("#/", "") || defaultView);
  return { show };
}
