/* Recruitment Desk — the route table.

   Deliberately NOT routes: the ⌘K palette, the notifications panel, the sidebar
   fold, row menus, every confirm and staged dialog, the applications list's
   filters and tabs, the stepper's step selection, the comment composer and its
   replies, the scorecard drawer. They are states of a screen; a breadcrumb that
   wants one of them is a bug in the screen, not a missing route.

   Nine top-level destinations, dense record work, four per-destination counts:
   this product runs the SIDEBAR shell, labelled mode, in TWO groups — the
   reviewing a recruiter does all day, and the running of the hiring itself. Two
   groups, so both carry a label (AGENTS §6.3).

   One application is one record with one route: /applications/:id is reached
   from the desk, the queue, the interview ladder, the requisition board, the
   talent pool and the palette alike. There is no second candidate screen. */

const RD_NAV = [
  { path: "/", label: "Your desk", icon: "layout-dashboard", group: "review" },
  { path: "/applications", label: "Applications", icon: "inbox", badge: "awaitingUs", group: "review" },
  { path: "/interviews", label: "Interviews", icon: "calendar-clock", badge: "feedbackOutstanding", group: "review" },
  { path: "/offers", label: "Offers", icon: "file-signature", badge: "offersExpiring", group: "review" },

  { path: "/requisitions", label: "Requisitions", icon: "briefcase", group: "hiring" },
  { path: "/compliance", label: "Compliance", icon: "shield-check", badge: "complianceUrgent", group: "hiring" },
  { path: "/pool", label: "Talent pool", icon: "users-round", group: "hiring" },
  { path: "/insights", label: "Insights", icon: "chart-no-axes-column", group: "hiring" },
  { path: "/team", label: "Desk team", icon: "user-round-cog", group: "hiring" },

  /* Registered, not in the nav. */
  { path: "/applications/new", label: "New application", parent: "/applications", offSidebar: true, group: "Create" },
  { path: "/applications/:id", label: "Application", parent: "/applications", offSidebar: true },
  { path: "/requisitions/new", label: "New requisition", parent: "/requisitions", offSidebar: true, group: "Create" },
  { path: "/requisitions/:id", label: "Requisition", parent: "/requisitions", offSidebar: true },
  /* The advert is a view WITHIN a requisition, so it is a registered child
     reached by the record's own tabs — not a destination of its own. */
  { path: "/requisitions/:id/advert", label: "Advert", parent: "/requisitions/:id", offSidebar: true },
  { path: "/me/profile", label: "Your profile", offSidebar: true },
  /* The notifications inbox is app-scope, not a destination: it is opened from
     the bell the count sits on (AGENTS §6.2), so it is registered off the nav. */
  { path: "/notifications", label: "Notifications", offSidebar: true },

  /* Settings is a NAMESPACE with no screen of its own: clicking it in the
     account menu opens the first child, and it supplies the first breadcrumb
     segment. Its children are the four things a desk actually configures. */
  { path: "/settings", label: "Settings", menuOnly: true, offSidebar: true },
  { path: "/settings/desk", label: "The desk", parent: "/settings", offSidebar: true, group: "Settings" },
  { path: "/settings/process", label: "Process and service levels", parent: "/settings", offSidebar: true, group: "Settings" },
  { path: "/settings/channels", label: "Adverts and channels", parent: "/settings", offSidebar: true, group: "Settings" },
  { path: "/settings/people", label: "People and access", parent: "/settings", offSidebar: true, group: "Settings" },

  /* Auth namespace — no navigation owner, no product chrome (AGENTS §7.1). */
  { path: "/signin", label: "Sign in", offSidebar: true },
  { path: "/signout", label: "Sign out", offSidebar: true },
];

const RD_ROUTES = indexRoutes(RD_NAV);

/* Projections the shell renders — derived by filtering the tree, never retyped. */
const destinations = () => RD_NAV.filter((r) => !r.offSidebar);
const NAV_GROUPS = [
  { key: "review", label: "Reviewing" },
  { key: "hiring", label: "Running the hiring" },
];
const sidebarGroups = () => NAV_GROUPS
  .map((group) => ({ label: group.label, items: destinations().filter((r) => r.group === group.key) }))
  .filter((group) => group.items.length > 0);

const accountMenu = () => [
  RD_ROUTES["/me/profile"], RD_ROUTES["/notifications"],
  /* Labelled by where it goes, not by the child that happens to be first. */
  Object.assign({}, RD_ROUTES["/settings/desk"], { label: "Settings" }),
  RD_ROUTES["/signout"],
];
/* The settings sub-nav: the route table filtered by parent, never retyped. */
const settingsPages = () => RD_NAV.filter((route) => route.parent === "/settings");

/* Paths that render without product chrome. */
const RD_AUTH_PATHS = ["/signin", "/signout"];

const resolvePath = (pattern, params) => pattern.replace(/:([^/]+)/g, (_, key) => params[key]);

/* The preview menu's page list — this table read whole and grouped, with the
   parameterised routes resolved against the fixture so a link lands on a real
   record rather than on ":id". A pattern with no fixture example is left out. */
const RD_PAGES = (data) => {
  const application = data.applications[0], requisition = data.requisitions[0];
  return templatePages(RD_NAV, RD_AUTH_PATHS, {
    "/applications/:id": application ? resolvePath("/applications/:id", { id: application.id }) : null,
    "/requisitions/:id": requisition ? resolvePath("/requisitions/:id", { id: requisition.id }) : null,
    "/requisitions/:id/advert": requisition ? resolvePath("/requisitions/:id/advert", { id: requisition.id }) : null,
  });
};

Object.assign(window, {
  RD_NAV, RD_ROUTES, RD_AUTH_PATHS, destinations, sidebarGroups, accountMenu, settingsPages, resolvePath, RD_PAGES,
});
