---
export interface Props {
  currentRoute?: string;
  currentModule?: string;
  currentClass?: string;
}

import { getVersionsData } from "@config/io/generateTypeData";
import { getGuideCollection } from "@config/io/guides";
import type { TreeEntry } from "./Tree.astro";
import Tree from "./Tree.astro";
import Link from "./Link.astro";

const versions = await getVersionsData();
const versionName = Astro.params.version;
const modules = versions.versions.find(version => version.name === versionName)?.modules;

const currentPath = Astro.url.pathname.split('/').filter(s => s !== "");

const guidePages = await getGuideCollection(versionName ?? "");

interface NavTree {
  title: string,
  slug: string,
  entries?: NavTree[],
}

function mkTree(mount: string, pathIdx: number, { title, slug, entries }: NavTree): TreeEntry {
  const link = `${mount}/${slug}`;

  return {
    title,
    link,
    current: currentPath[pathIdx] === slug,
    entries: entries?.map(entry => mkTree(link, pathIdx + 1, entry)),
  };
}

function genGuideNav(base: string): NavTree[] | undefined {
  const pages = guidePages
    .filter(page => page.id.match(`^${base}[^/]*$`) !== null && page.id !== "index")
    .sort((a, b) => a.data.index - b.data.index)
    .map(page => ({
      title: page.data.title,
      slug: page.id,
      entries: genGuideNav(page.id + "/"),
    }));

  return pages.length === 0 ? undefined : pages;
}

let versionedEntries;
let versionsTree;

if (versionName) {
  versionedEntries = {
    guide: mkTree(`/docs/${versionName}`, 2, {
      title: "Usage Guide",
      slug: "guide",
      entries: genGuideNav(""),
    }),
    types: mkTree(`/docs/${versionName}`, 2, {
      title: "Quickshell Types",
      slug: "types",
      entries: modules!.map(module => ({
        title: module.name,
        slug: module.name,
        entries: module.types.map(type => ({
          title: type.name,
          slug: type.name,
        }))
      }))
    }),
  };

  versionsTree = {
    title: `Switch Version (${versionName})`,
    link: "#",
    entries: versions.versions.map(version => ({
      title: version.name,
      link: `/docs/${version.name}${Astro.url.pathname.slice(6 + versionName.length)}`,
      current: version.name == versionName,
    })),
  };
}
---
<nav class="navtree">
  <Link
    title="About"
    link="/about"
    current={currentPath.length === 1 && currentPath[0] === "about"}
  />
  <Link
    title="Changelog"
    link="/changelog"
    current={currentPath.length === 1 && currentPath[0] === "changelog"}
  />
  { versionedEntries && <Tree {...versionsTree as TreeEntry}/>}
  <hr/>
  { versionedEntries && (
    <Tree {...versionedEntries.guide}/>
    <Tree {...versionedEntries.types}/>
    <Link
      title="QtQuick Types"
      link="https://doc.qt.io/qt-6/qtquick-qmlmodule.html"
      showIcon={true}
    />
    <Link
      title="Quickshell Examples"
      link="https://git.outfoxxed.me/outfoxxed/quickshell-examples"
      showIcon={true}
    />
  )}
  { !versionedEntries && versions.versions.map(version => (
    <Link
      title={`Quickshell Documentation (${version.name})`}
      link={`/docs/${version.name}/guide`}
    />
  ))}
</nav>
