/*
 * @Author: wfl
 * @LastEditors: wfl
 * @description:
 * @updateInfo:
 * @Date: 2022-07-29 11:45:14
 * @LastEditTime: 2022-09-14 17:18:00
 */
import type { Menu, MenuModule } from '@/mainApp/router/types';

import { getAllParentPath, transformMenuModule } from '@/mainApp/router/helper/menuHelper';
import { usePermissionStore } from '@/global/store/modules/permission';

const modules = import.meta.globEager('./modules/**/*.ts');

const menuModules: MenuModule[] = [];

Object.keys(modules).forEach((key) => {
  const mod = modules[key].default || {};
  const modList = Array.isArray(mod) ? [...mod] : [mod];
  menuModules.push(...modList);
});

// ===========================
// ==========Helper===========
// ===========================

const staticMenus: Menu[] = [];
(() => {
  menuModules.sort((a, b) => {
    return (a.orderNo || 0) - (b.orderNo || 0);
  });

  for (const menu of menuModules) {
    staticMenus.push(transformMenuModule(menu));
  }
})();

async function getAsyncMenus() {
  const permissionStore = usePermissionStore();
  // getsystemMenuList
  return permissionStore.getsystemMenuTree.filter((item) => {
    return !item.meta?.hideMenu && !item.hideMenu;
  });
}

export const getMenus = async (): Promise<Menu[]> => {
  const menus = await getAsyncMenus();
  return menus;
};

export async function getCurrentParentPath(currentPath: string) {
  const menus = await getAsyncMenus();
  const allParentPath = await getAllParentPath(menus, currentPath);
  return allParentPath?.[0];
}

// Get the level 1 menu, delete children
export async function getShallowMenus(): Promise<Menu[]> {
  const menus = await getAsyncMenus();
  return menus.map((item) => ({ ...item, children: undefined }));
  // const routes = router.getRoutes();
  // return shallowMenuList.filter(basicFilter(routes));
}

// Get the children of the menu
export async function getChildrenMenus(parentPath: string) {
  const menus = await getMenus();
  const parent = menus.find(
    (item) => item.path === parentPath || (item.__nodekey + '').startsWith(parentPath + ''),
  );
  if (!parent || !parent.children || !!parent?.meta?.hideChildrenInMenu) {
    return [] as Menu[];
  }
  // const routes = router.getRoutes();
  return parent.children; //filter(parent.children, basicFilter(routes));
}

// function basicFilter(routes: RouteRecordNormalized[]) {
//   return (menu: Menu) => {
//     const matchRoute = routes.find((route) => {
//       if (paramType.isUrl(menu.path)) return true;

//       if (route.meta?.carryParam) {
//         return pathToRegexp(route.path).test(menu.path);
//       }
//       const isSame = route.path === menu.path;
//       if (!isSame) return false;

//       if (route.meta?.ignoreAuth) return true;

//       return isSame || pathToRegexp(route.path).test(menu.path);
//     });

//     if (!matchRoute) return false;
//     menu.icon = (menu.icon || matchRoute.meta.icon) as string;
//     menu.meta = matchRoute.meta;
//     return true;
//   };
// }
