import config from "@/lib/listPermission.json"; export interface PermissionNode { key: string; label: string; children?: PermissionNode[]; } interface Grouped { [key: string]: { label: string; children: Grouped; actions: string[]; }; } /* --- Build lookup table --- */ const permissionMap: Record = {}; function walk(nodes: PermissionNode[], path: string[] = []) { nodes.forEach((n) => { const full = [...path, n.label]; permissionMap[n.key] = full; if (n.children) walk(n.children, full); }); } walk(config.menus); /* --- Convert keys → hierarchical grouped --- */ export function groupPermissions(keys: string[]) { const tree: Grouped = {}; keys.forEach((key) => { const path = permissionMap[key]; if (!path) return; let pointer = tree; path.forEach((label, idx) => { if (!pointer[label]) { pointer[label] = { label, children: {}, actions: [] }; } // last item = actual permission action if (idx === path.length - 1) { pointer[label].actions.push(label); } pointer = pointer[label].children; }); }); return tree; }