import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import { resolve } from "node:path";

const appVuePath = resolve("frontend/src/App.vue");
const source = readFileSync(appVuePath, "utf8");

const navStart = source.indexOf("const navStages = [");
const navEnd = source.indexOf("\n];", navStart);

assert.notEqual(navStart, -1, "未找到 navStages 菜单配置");
assert.notEqual(navEnd, -1, "未找到 navStages 结束位置");

const navSource = source.slice(navStart, navEnd);
const iconEntries = [];
const entryPattern = /label:\s*"([^"]+)"[\s\S]*?iconPaths:\s*\[([\s\S]*?)\]/g;

let match;
while ((match = entryPattern.exec(navSource))) {
  const [, label, iconSource] = match;
  const paths = [...iconSource.matchAll(/"([^"]+)"/g)].map((pathMatch) => pathMatch[1].trim());
  if (paths.length) {
    iconEntries.push({
      label,
      signature: paths.join("|")
    });
  }
}

assert.ok(iconEntries.length > 0, "未解析到任何侧边栏图标配置");

const labelsBySignature = new Map();
for (const entry of iconEntries) {
  const labels = labelsBySignature.get(entry.signature) || [];
  labels.push(entry.label);
  labelsBySignature.set(entry.signature, labels);
}

const duplicates = [...labelsBySignature.values()].filter((labels) => labels.length > 1);

assert.deepEqual(
  duplicates,
  [],
  `侧边栏存在重复图标：${duplicates.map((labels) => labels.join(" / ")).join("；")}`
);

console.log(`侧边栏图标唯一性检查通过，共检查 ${iconEntries.length} 个菜单图标。`);
