export interface MenuSubItem {
  name: string;
  routeName: string;
  children?: MenuSubItem[];
}

type MenuItem = {
  code: string;
} & MenuSubItem;

export const menuMap: MenuItem[] = [
  {
    code: "STU_ONLINE_EXAM",
    name: "在线考试",
    routeName: "OnlineExam",
  },
  {
    code: "STU_ONLINE_HOMEWORK",
    name: "在线作业",
    routeName: "OnlineHomework",
  },
  {
    code: "STU_ONLINE_PRACTICE",
    name: "在线练习",
    routeName: "OnlinePractice",
    children: [
      {
        name: "练习详情",
        routeName: "OnlinePracticeRecord",
        children: [
          {
            name: "成绩报告",
            routeName: "OnlinePracticeRecordDetail",
          },
        ],
      },
    ],
  },
  {
    code: "STU_OFFLINE_EXAM",
    name: "离线考试",
    routeName: "OfflineExam",
  },
  {
    code: "STU_NOTICE",
    name: "公告通知",
    routeName: "SiteMessage",
    children: [
      {
        name: "通知详情",
        routeName: "SiteMessageDetail",
      },
    ],
  },
  {
    code: "STU_MODIFY_PWD",
    name: "修改密码",
    routeName: "ChangePassword",
  },
];

export function getMenuDict() {
  const menuDict: Record<string, string> = {};
  menuMap.forEach((menu) => {
    menuDict[menu.code] = menu.routeName;
  });
  return menuDict;
}

export function parseMapLine(routeName: string) {
  let finded = false;
  let mapLine: MenuSubItem[] = [];
  const parseTree = (list: MenuSubItem[], parents: MenuSubItem[]) => {
    list.forEach((item) => {
      if (finded) return;
      const curParent = [...parents];
      curParent.push({
        name: item.name,
        routeName: item.routeName,
      });
      if (item.routeName === routeName) {
        finded = true;
        mapLine = curParent;
        return;
      }
      if (item.children && item.children.length) {
        parseTree(item.children, curParent);
      }
    });
  };
  parseTree(menuMap, []);
  return mapLine;
}
