import type { SidebarLink } from "@/app/lib/aclSidebar"; import { INVENTORY_MODULE_NAMES } from "@/app/lib/inventoryModules"; // Presentation merges over the aclMenu-derived sidebar (module-id-category-order). Order and // grouping come from the module tree as given (siblings in id order); this never reorders and // never grants access. // // Merged entries (consolidate-admin-settings-nav): paired pages collapse into one link to the first // granted member, at the first present member's position; the pages link to each other through // SectionTabs. const MERGES: { merge: string[]; label: string }[] = [ { merge: ["Locations & Warehouses", "BO_WAREHOUSE_SETUP"], label: "BO_NAV_LOCATIONS_WAREHOUSES" }, { merge: ["BO_LOCALES", "BO_MESSAGES"], label: "BO_NAV_LANGUAGES" }, ]; const INVENTORY_LINK: SidebarLink = { name: "BO_NAV_INVENTORY", url: "/inventory", iconName: "Box", exact: true, children: [] }; /** merge-inventory-page: a group holding only inventory modules is one top-level Inventory link. */ const isInventoryGroup = (l: SidebarLink) => l.url === "" && l.children.length > 0 && l.children.every((c) => INVENTORY_MODULE_NAMES.includes(c.name)); /** Collapses the granted members into one link to the first; the rest become activeUrls. */ function mergedLink(label: string, granted: SidebarLink[]): SidebarLink { const [first, ...rest] = granted; return { name: label, url: first.url, ...(first.iconName && { iconName: first.iconName }), children: [], activeUrls: rest.map((l) => l.url), }; } /** Applies the presentation merges at every level, preserving input order. */ export function applySidebarLayout(links: SidebarLink[]): SidebarLink[] { const emitted = new Set(); return links.flatMap((l) => { if (isInventoryGroup(l)) return [INVENTORY_LINK]; if (l.url === "") return [{ ...l, children: applySidebarLayout(l.children) }]; const m = MERGES.find((e) => e.merge.includes(l.name)); if (!m) return [l]; if (emitted.has(m.label)) return []; emitted.add(m.label); return [mergedLink(m.label, links.filter((s) => m.merge.includes(s.name)))]; }); }