import type { RouteRecordRaw } from 'vue-router';
import { isArray } from 'lodash-es';
import { appConfig } from '/@/config/app.config.ts';
const modules = import.meta.globEager('/src/router/**/*.ts');

// const jsonModules = import.meta.globEager('/src/data/router/*.json');

const routers: any[] = [];
// Object.keys(modules).forEach((key) => {
//   const mod = modules[key].default || {};
//   const modList = Array.isArray(mod) ? [...mod] : [mod];
//   modList[0].path && routers.push(...modList);
// });

// Object.keys(jsonModules).forEach((key) => {
//   const mod = jsonModules[key].default || {};
//   const modList = Array.isArray(mod) ? [...mod] : [mod];
//   modList.forEach((item, index) => {
//     modList[index] = setComponent(item);
//   });
//   routers.push(...modList);
// });

/**
 * 检测路由中是否包含name为root的路由（root是默认路由）
 * @param routers
 */
function hasRoot(routers: RouteRecordRaw[]): boolean {
  let root = undefined;
  function checkRoot(route: RouteRecordRaw | RouteRecordRaw[]) {
    if (isArray(route)) {
      for (let i = 0, l = route.length; i < l; i++) {
        checkRoot(route[i]);
      }
    } else {
      if (route.name === 'root') {
        return (root = route);
      }
      if (route.children) {
        for (let i = 0, l = route.children.length; i < l; i++) {
          checkRoot(route.children[i]);
        }
      }
    }
  }
  checkRoot(routers);
  return !!root;
}
// if (!hasRoot(routers)) console.error('Route named root is required  as the default route');

function setComponent(route: any) {
  /* @vite-ignore */
  const component = route.component;
  if (component) {
    route.component = () =>
      import(/* @vite-ignore */ `/nerv-lib/paas/view/service/${component}.vue`);
  } else {
    route.component = () =>
      import(/* @vite-ignore */ '/nerv-lib/paas/view/system/layout/content.vue');
  }
  if (route.children) {
    route.children.forEach((item: Object, index: number) => {
      route.children[index] = setComponent(item);
    });
  }
  return route;
}

/**
 * 登陆页面路由
 */
export const LoginRoute = {
  path: '/login',
  name: 'login',
  component: appConfig.customLogin
    ? appConfig.customLogin
    : () => import('/nerv-lib/paas/view/system/login.vue'),
  meta: {
    title: '登录',
  },
};

/**
 * 页面不存在
 */
export const Error404Route = {
  path: '/:pathMatch(.*)*',
  name: 'error404',
  component: () => import('/nerv-lib/paas/view/service/error-404.vue'),
};

/**
 * 页面无权限
 */
export const Error403Route = {
  path: '/403',
  name: 'error403',
  component: () => import('/nerv-lib/paas/view/service/error-403.vue'),
};

/**
 * 默认路由 转发至root
 */
export const RootRoute = {
  path: '/',
  name: 'rootRoute',
  redirect: 'root',
};
const outControlPageName: any[] = [];
Object.keys(modules).forEach((key) => {
  const mod = modules[key].default || {};
  if (mod.name && mod.outContrl) {
    outControlPageName.push(mod.name);
  }
});
export const WHITE_NAME_LIST = [
  Error404Route.name,
  Error403Route.name,
  LoginRoute.name,
  ...outControlPageName,
];

export const ALLRoute = [...routers, LoginRoute, Error403Route, Error404Route, RootRoute];
