import {
  buildSidebarSections,
  type SidebarMenuInput,
} from "../src/navigation/sidebarMenu";

const backendMenus: SidebarMenuInput[] = [
  { code: "attendance_realtime", name: "实时考勤", path: "/attendance/realtime" },
  { code: "monthly_payroll", name: "月度核算", path: "/payroll/monthly" },
  { code: "payroll", name: "工资计算", path: "/payroll" },
  { code: "jobs", name: "计算记录", path: "/payroll/jobs" },
  { code: "commissions", name: "提成管理", path: "/payroll/commissions" },
  { code: "employees", name: "员工维护", path: "/system/employees" },
  { code: "salary_profiles", name: "薪资维护", path: "/system/salary-profiles" },
  { code: "organization", name: "组织架构", path: "/system/organization" },
  { code: "reports", name: "统计报表", path: "/reports" },
  { code: "configs", name: "规则配置", path: "/system/configs" },
  { code: "users", name: "用户管理", path: "/system/users" },
  { code: "operation_logs", name: "操作日志", path: "/system/operation-logs" },
];

const sections = buildSidebarSections(backendMenus);

assertEqual(
  sections.map((section) => section.title).join(","),
  "核心工作,月度核算,员工薪资,报表分析,系统管理",
);

assertEqual(sectionCodes("核心工作"), "dashboard,attendance_realtime");
assertEqual(sectionCodes("月度核算"), "monthly_payroll,commissions");
assertEqual(sectionNames("月度核算"), "工资核算,提成录入");
assertEqual(sectionCodes("员工薪资"), "employees,salary_profiles,organization");
assertEqual(sectionCodes("报表分析"), "reports");
assertEqual(sectionCodes("系统管理"), "configs,users,operation_logs");
assertEqual(hasMenu("payroll"), "false");
assertEqual(hasMenu("jobs"), "false");

function sectionCodes(title: string): string {
  const section = sections.find((item) => item.title === title);
  if (!section) {
    throw new Error(`找不到菜单分组：${title}`);
  }
  return section.items.map((item) => item.code).join(",");
}

function sectionNames(title: string): string {
  const section = sections.find((item) => item.title === title);
  if (!section) {
    throw new Error(`找不到菜单分组：${title}`);
  }
  return section.items.map((item) => item.name).join(",");
}

function assertEqual(actual: string, expected: string): void {
  if (actual !== expected) {
    throw new Error(`期望 ${expected}，实际 ${actual}`);
  }
}

function hasMenu(code: string): string {
  return String(sections.some((section) => section.items.some((item) => item.code === code)));
}
