import {RouteRecordRaw} from "vue-router";
import {SYSTEM_MENUS} from "@/config";
import {SystemMenu} from "@/types/config";

const routerClassify: Record<'layout' | 'full', RouteRecordRaw[]> = {
    layout: [],
    full: []
}

/**
 * 提取菜单路由
 */
const extractMenuToRouter = () => {
    const traverse = (data: SystemMenu[]) => {
        data.forEach(item => {
            if (item.type === 'dir' && item.children && item.children.length > 0) {
                traverse(item.children)
            } else {
                if (!item.isFull) {
                    routerClassify.layout.push({
                        path: item.path,
                        name: item.name,
                        meta: {
                            title: item.title
                        },
                        component: item.component
                    } as RouteRecordRaw)
                } else {
                    routerClassify.full.push({
                        path: item.path,
                        name: item.name,
                        meta: {
                            title: item.title
                        },
                        component: item.component
                    } as RouteRecordRaw)
                }
            }
        })
    }
    traverse(SYSTEM_MENUS)
}

extractMenuToRouter()

export const staticRouter: RouteRecordRaw[] = [
    {
        path: '/login',
        name: 'login',
        meta: {
            title: '登录',
        },
        component: () => import("@/views/login.vue"),
    }, {
        path: "/",
        redirect: '/index',
    }, {
        path: '/layout',
        name: 'layout',
        redirect: '/index',
        component: () => import("@/components/layout/Layout.vue"),
        children: routerClassify.layout
    },
    ...routerClassify.full,
    {
        path: '/test',
        name: 'test',
        component: () => import("@/views/test.vue"),
    },
]
