import router from "./index";
import { basicRoutes as routes } from "./routes";
const modules = import.meta.glob([
    "../views/*.vue",
    "../views/*/*.vue",
    "../views/*/*/*.vue",
    "../components/layout/index.vue",
]);

export const addDynamicRoutes = async (data) => {
    if (!data || !data.length) return;
    const homeRouters = routes.filter(v => v.path === '/main')[0];
    if (!homeRouters) return;
    homeRouters.children = [];
    homeRouters.children.push({
        path: '/home',
        name: 'Home',
        component: () => import('../views/Home.vue'),
    });
    homeRouters.children.push({
        path: '/settings',
        name: 'Settings',
        component: () => import('../views/Settings.vue'),
    });
    data.forEach(item => {
        homeRouters.children.push({
            path: item.path,
            name: item.name,
            component: modules[`../views/${item.component}.vue`],
        })
    })
    homeRouters.children.push({
        path: '/:pathMatch(.*)',
        name: 'NotFound',
        component: () => import('../views/404.vue'),
    });
    homeRouters.children.push({
        path: '/error-page',
        name: 'error-page',
        component: () => import('../views/404.vue'),
    });
    router.addRoute(homeRouters);
}
