import type { App } from 'vue'
import type { RouteRecordName, RouteRecordRaw } from 'vue-router'
import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'
import { authentication, base, disableRouterWarning, isHashRouterMode } from '/@/config'
import { setupPermissions } from '/@/router/permissions'
import Layout from '/@vab/layouts/index.vue'

export const constantRoutes: any = [
  {
    path: '/login', name: 'Login', component: () => import('/@/views/login/Login.vue'),
    meta: { hidden: true },
  },
  {
    path: '/redirect', name: 'Redirect', component: () => import('/@/views/redirect/Redirect.vue'),
    meta: { hidden: true },
  },
  {
    path: '/403', name: '403', component: () => import('/@/views/error/403.vue'),
    meta: { hidden: true },
  },
  {
    path: '/404', name: '404', component: () => import('/@/views/error/404.vue'),
    meta: { hidden: true },
  },
  {
    path: '/', name: 'Root', component: Layout, meta: { title: '首页', icon: 'home-2-line' },
    children: [
      {
        path: 'index', name: 'Index', component: () => import('/@/views/index/index.vue'),
        meta: { title: '首页', icon: 'home-2-line', noClosable: true, noKeepAlive: true }
      },
      // {
      //   path: 'monitor', name: 'Monitor', component: () => import('/@/views/index/monitor.vue'),
      //   meta: { title: '监控视频', icon: 'home-2-line', noClosable: true, noKeepAlive: true }
      // },
      // {
      //   path: 'RouteMenu', name: 'RouteMenu', component: () => import('/@/views/SystemConfiguration/routeMenu/index.vue'),
      //   meta: { title: '路线权限管理', icon: 'admin-line', noClosable: true, noKeepAlive: true }
      // }
    ]
  }
]

export const asyncRoutes: any = []

const router = createRouter({
  history: isHashRouterMode ? createWebHashHistory(base) : createWebHistory(base),
  routes: constantRoutes as RouteRecordRaw[],
})

const fatteningRoutes = (routes: VabRouteRecord[]): VabRouteRecord[] => {
  return routes.flatMap((route) => {
    return route.children ? fatteningRoutes(route.children) : route
  })
}

const addRouter = (routes: VabRouteRecord[]) => {
  routes.forEach((route: VabRouteRecord) => {
    if (!router.hasRoute(route.name)) router.addRoute(route as RouteRecordRaw)
    if (route.children) addRouter(route.children)
  })
}

export const resetRouter = (routes: VabRouteRecord[] = constantRoutes) => {
  routes.map((route: VabRouteRecord) => {
    if (route.children) route.children = fatteningRoutes(route.children)
  })
  router.getRoutes().forEach((route) => {
    if (route.name) {
      const routeName: RouteRecordName = route.name
      router.hasRoute(routeName) && router.removeRoute(routeName)
    }
  })
  addRouter(routes)
}

export const setupRouter = (app: App<Element>) => {
  if (disableRouterWarning)
    router.addRoute({
      path: '/:pathMatch(.*)*',
      component: () => {},
    })

  if (authentication === 'intelligence') addRouter(asyncRoutes)
  setupPermissions(router)
  app.use(router)

  return router
}

export default router
