import type { AppRouteRecordRaw, AppRouteModule } from '@/router/types';

import { PAGE_NOT_FOUND_ROUTE, REDIRECT_ROUTE } from '@/router/routes/basic';
import { LAYOUT } from '@/router/constant';

import { mainOutRoutes } from './mainOut';
import { PageEnum } from '@/enums/pageEnum';
import { t } from '@/hooks/web/useI18n';

// import.meta.glob() 直接引入所有的模块 Vite 独有的功能
const modules = import.meta.glob('./modules/**/*.ts', { eager: true });
const routeModuleList: AppRouteModule[] = [];

// 加入到路由集合中
Object.keys(modules).forEach((key) => {
  const mod = (modules as Recordable)[key].default || {};
  if (mod.meta) {
    const modList = Array.isArray(mod) ? [...mod] : [mod];
    routeModuleList.push(...modList);
  }
});

export const asyncRoutes = [PAGE_NOT_FOUND_ROUTE, ...routeModuleList];

// 根路由
export const RootRoute: AppRouteRecordRaw = {
  path: '/',
  name: 'Root',
  redirect: PageEnum.BASE_HOME,
  // redirect: '/home/index',
  meta: {
    title: '首页',
  },
};

export const LoginRoute: AppRouteRecordRaw = {
  path: '/login',
  name: 'Login',
  // component: () => import('@/views/sys/login/Login.vue'),
  component: () => import('@/views/sys/login/Login.vue'),
  meta: {
    title: t('routes.basic.login'),
  },
};
export const HomeRoute: AppRouteRecordRaw = {
  path: '/home',
  name: 'Home',
  component: () => import('@/views/home/index.vue'),
  meta: {
    title: '首页',
  },
};
export const FORMCALLPAGE_ROUTE: AppRouteRecordRaw = {
  path: '/formCallPageParent',
  component: LAYOUT,
  name: 'formCallPageParent',
  meta: {
    title: '表单',
    hideBreadcrumb: true,
    hideMenu: true,
  },
  children: [
    {
      path: '/formCallPage',
      name: 'formCallPage',
      component: () => import('@/views/demo/onlineform/formCall/index.vue'),
      meta: {
        title: '表单调用',
        hideBreadcrumb: true,
      },
    },
  ],
};
// Basic routing without permission
// 未经许可的基本路由
export const basicRoutes = [
  LoginRoute,
  HomeRoute,
  RootRoute,
  FORMCALLPAGE_ROUTE,
  ...mainOutRoutes,
  REDIRECT_ROUTE,
  PAGE_NOT_FOUND_ROUTE,
];
