import type { LucideIcon } from "lucide-react";
import {
  CalendarClock,
  LayoutDashboard,
  Network,
  Settings,
  SlidersHorizontal,
  Wallet,
} from "lucide-react";

import type { AdminNavGroup, AdminNavItem } from "@/modules/_config/admin-nav";

export const ADMIN_NAV_GROUP_ICON: Record<AdminNavGroup, LucideIcon> = {
  overview: LayoutDashboard,
  agent: Network,
  operations: CalendarClock,
  finance: Wallet,
  rules: SlidersHorizontal,
  platform: Settings,
};

/** 与 Laravel {@link AdminAuthorizationRegistry::NAV_GROUP_ORDER} 一致 */
export const ADMIN_NAV_GROUP_ORDER: readonly AdminNavGroup[] = [
  "overview",
  "agent",
  "operations",
  "finance",
  "rules",
  "platform",
] as const;

export function groupAdminNavItems(
  items: readonly AdminNavItem[],
): { group: AdminNavGroup; items: AdminNavItem[] }[] {
  const buckets = new Map<AdminNavGroup, AdminNavItem[]>();

  for (const item of items) {
    const group = item.nav_group ?? "operations";
    const list = buckets.get(group) ?? [];
    list.push(item);
    buckets.set(group, list);
  }

  return ADMIN_NAV_GROUP_ORDER.filter((group) => buckets.has(group)).map((group) => ({
    group,
    items: buckets.get(group)!,
  }));
}
