import { infoPages } from "./info";
import { barcampParticipantsSheetUrl } from "./barcamp";
import {
  getEventBasePath,
  getEventPageEntries,
  type EventPageEntry,
  type Event,
} from "./events";

function getSectionGroup(page: EventPageEntry) {
  const planning = new Set([
    "workshops",
    "important-dates",
    "registration-logistics",
    "contact",
    "registration",
    "workshop-registration",
    "registration-notes",
    "socials-at-fsttcs-2024",
  ]);
  const travel = new Set([
    "internet-access-via-wifi",
    "accommodation-for-students",
    "guest-house-and-hotel-accommodation",
    "fortune-inn-haveli",
    "for-international-participants",
    "international-visitor-notes",
    "reaching-and-leaving-iit-gandhinagar",
  ]);

  if (planning.has(page.id)) return "Planning";
  if (travel.has(page.id)) return "Travel & Stay";
  if (page.id !== "overview") return "Venue & Campus";
  return undefined;
}

export function getEventNavItems(event: Event) {
  const basePath = getEventBasePath(event);
  const eventPages = getEventPageEntries(event);
  const sectionPages = eventPages.filter((page) => page.type === "section");
  const schedulePage = eventPages.find((page) => page.type === "schedule");
  const peoplePage = eventPages.find((page) => page.type === "people");
  const visualPage = eventPages.find((page) => page.type === "visual");

  if (event.slug === "ncert") {
    const directSectionIds = ["participants", "summary-minutes"];

    return [
      ...(!event.hideOverviewNav ? [{ label: "Overview", href: basePath }] : []),
      ...directSectionIds.flatMap((id) => {
        const page = sectionPages.find((entry) => entry.id === id);
        return page ? [{ label: page.label, href: page.href }] : [];
      }),
      ...(schedulePage ? [{ label: "Schedule", href: schedulePage.href }] : []),
      {
        label: "Information",
        items: infoPages.map((page) => ({
          label: page.title,
          href: `${basePath}info/${page.slug}/`,
        })),
      },
    ];
  }

  if (event.slug === "barcamp") {
    return [
      ...(!event.hideOverviewNav ? [{ label: "Overview", href: basePath }] : []),
      ...(peoplePage
        ? [{ label: "Participants", href: barcampParticipantsSheetUrl, external: true }]
        : []),
      ...(schedulePage ? [{ label: "Schedule", href: schedulePage.href }] : []),
      {
        label: "Venue",
        items: infoPages.map((page) => ({
          label: page.title,
          href: `${basePath}info/${page.slug}/`,
        })),
      },
    ];
  }

  return [
    ...(!event.hideOverviewNav ? [{ label: "Overview", href: basePath }] : []),
    ...(sectionPages.length
      ? [
          {
            label: "Details",
            items: sectionPages.map((page) => ({
              label: page.label,
              href: page.href,
              group: sectionPages.length >= 16 ? getSectionGroup(page) : undefined,
            })),
          },
        ]
      : []),
    ...(schedulePage ? [{ label: "Schedule", href: schedulePage.href }] : []),
    ...(peoplePage ? [{ label: "Participants", href: peoplePage.href }] : []),
    ...(visualPage ? [{ label: "Visuals", href: visualPage.href }] : []),
    {
      label: "Information",
      items: infoPages.map((page) => ({
        label: page.title,
        href: `${basePath}info/${page.slug}/`,
      })),
    },
    ...(event.sourceUrl
      ? [{ label: "Event website", href: event.sourceUrl, external: true }]
      : []),
  ];
}
