import { staticRoutes } from "@/router/constantRoutes"
import { dynamicRoutes } from "@/router/dynamicRoutes"

const Micro = () => import("@/micro/index.vue")
import { useUserStore } from "@/store/user.js"
import Layout from "@/layout/index.vue"
import ParentView from "@/components/ParentView/index.vue"

const modules = import.meta.glob("./../views/**/*.vue")
export const usePermissionStore = defineStore(
    "permission",
    {
        state: () => ({
            routes: [],
            addRoutes: [],
            sidebarRouters: [],
            defaultRoutes: []
        }),
        actions: {
            setRoutes(routes) {
                this.addRoutes = routes
                this.routes = staticRoutes.concat(routes)
            },
            setSidebarRouters(routes) {
                this.sidebarRouters = routes
            },
            setDefaultRoutes(routes) {
                this.defaultRoutes = staticRoutes.concat(routes)
            },
            generateRoutes() {
                return new Promise(resolve => {
                    const sidebarRoutes = filterAsyncRouter(useUserStore().menuList[0].children[0].children)
                    this.setSidebarRouters(staticRoutes.concat(sidebarRoutes))
                    const rewriteRoutes = filterAsyncRouter(useUserStore().menuList[0].children[0].children, false)
                    this.setRoutes(rewriteRoutes)
                    resolve(rewriteRoutes)
                })
            }
        }
    }
)

function filterAsyncRouter(asyncRouterMap, httpSave = true) {
    const blackList = ["/system/dataList", "/progress"]
    const hideList = ["/publicDomain", "/caseShare"
        // "/workflow"
    ]

    let arr = []
    asyncRouterMap.filter(route => {
        if (!blackList.includes(route.path)) {
            if (route.children != null && route.children && route.children.length && route.children[0].type != 2 && route.parentId == "1762659463383281665") {
                let info = {
                    alwaysShow: true,
                    children: [],
                    component: Layout,
                    hidden: hideList.includes(route.path),
                    meta: {
                        icon: route.icon,
                        link: "",
                        cache: false,
                        title: route.name
                    },
                    name: route.name,
                    path: route.path,
                    redirect: false
                }
                info.children = filterAsyncRouter(route.children)
                if (info.children.length > 0) {
                    info.redirect = info.children[0].path
                }
                arr.push(info)
            } else if (route.children != null && route.children && route.children.length && route.children[0].type != 2 && route.parentId != "1762659463383281665") {
                let info = {
                    alwaysShow: true,
                    children: [],
                    component: ParentView,
                    hidden: hideList.includes(route.path),
                    meta: {
                        icon: route.icon,
                        link: "",
                        cache: false,
                        title: route.name
                    },
                    name: route.name,
                    path: route.path,
                    redirect: false
                }
                info.children = filterAsyncRouter(route.children)
                if (info.children.length > 0) {
                    info.redirect = info.children[0].path
                }
                arr.push(info)
            } else if (route.children.length == 0 && route.parentId == "1762659463383281665") {
                let info = {
                    path: "",
                    component: Layout,
                    redirect: route.path,
                    alwaysShow: false,
                    hidden: hideList.includes(route.path),
                    meta: {
                        icon: route.icon,
                        title: route.name
                    },
                    children: [
                        {
                            // alwaysShow: false, //菜单不支持设置
                            children: [],
                            component: loadView(route.path),
                            hidden: hideList.includes(route.path),
                            meta: {
                                icon: route.icon,
                                link: "",
                                cache: false,
                                title: route.name
                            },
                            name: route.name,
                            path: route.path,
                            redirect: false
                        }
                    ]

                }
                if (route.path.startsWith("http") || route.path.startsWith("/tjmchild") || route.path.includes("queryParams")) {
                    if (httpSave) {
                        // const prefix = "/tjmchild"
                        // if (route.path.startsWith(prefix)) {
                        //   info.redirect = route.path.slice(prefix.length)
                        //   info.children[0].path = route.path.slice(prefix.length)
                        // }
                        arr.push(info)
                    }
                } else {
                    arr.push(info)
                }
            } else if (route.type == 1) {
                let info = {
                    // alwaysShow: false, //菜单不支持设置
                    children: [],
                    component: loadView(route.path),
                    hidden: hideList.includes(route.path),
                    meta: {
                        icon: route.icon,
                        link: "",
                        cache: false,
                        title: route.name
                    },
                    name: route.name,
                    path: route.path,
                    redirect: false
                }
                if (route.path.startsWith("http") || route.path.startsWith("/tjmchild") || route.path.includes("queryParams")) {
                    if (httpSave) {
                        // const prefix = "/tjmchild"
                        // if (route.path.startsWith(prefix)) {
                        //   info.redirect = route.path.slice(prefix.length)
                        //   info.path = route.path.slice(prefix.length)
                        // }
                        arr.push(info)
                    }
                } else {
                    arr.push(info)
                }
            }
        }
    })
    return arr
}

function loadView(view) {
    for (const path in modules) {
        const dir = "/" + path.split("views/")[1].split(".vue")[0];
        if (dir === view || dir == view + "/index") {
            return modules[path]
        }
    }
}