import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'

const Layout = () => import('@/layouts/index.vue')

const constantRoutes: Array<RouteRecordRaw> = [
  { path: '/404', name: '404', component: () => import('@/views/error/404.vue') }
]

const asyncRoutes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Root',
    component: Layout,
    redirect: '/index',
    meta: {},
    children: [
      {
        path: '/index',
        name: 'TestCase',
        meta: { hidden: false, title: '测试用例' },
        component: () => import('@/views/test/index.vue')
      }
    ]
  },
  {
    path: '/test',
    name: 'test',
    component: Layout,
    meta: {},
    children: [
      {
        path: '/flow',
        name: 'Flow',
        meta: { hidden: false, title: '用例设计' },
        component: () => import('@/views/test/flow.vue')
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NoFound',
    redirect: '/404',
    meta: {
      hide: true
    }
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoutes
})

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

export function setupRouter(app: any) {
  // 开发路由
  addRouter(asyncRoutes)
  console.log(router.getRoutes())
  app.use(router)
  return router
}

export default router
