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

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

    traverse(SYSTEM_MENUS)

    return result;
}

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: extractMenuToRouter()
    }, {
        path: '/test',
        name: 'test',
        component: () => import("@/views/test.vue"),
    },
    {
        path: '/enterprise',
        name: 'enterprise',
        component: () => import("@/views/enterprise.vue"),
    },
]
