import { createRouter, RouteRecordRaw, createWebHistory } from 'vue-router'
import { PageEnum } from '@/enums/api'
import type { App } from 'vue'
import 'nprogress/nprogress.css'
import { appRoutes } from './routes'

export const WHITE_PATH_LIST: string[] = [
  PageEnum.LOGIN_PATH,
  PageEnum.REGISTER_PATH
]

/* 全局路由无需token校验 */
const globalRoute: RouteRecordRaw[] = [
  {
    path: '/:path(.*)*',
    component: () => import('@/layout/basic/default-layout.vue'),
    children: [
      {
        path: '/:path(.*)*',
        component: () => import('@/views/not-found/index.vue'),
        meta: {
          requiresAuth: false,
          noAffix: true,
          ignoreCache: false,
          title: '路径无效'
        }
      }
    ]
  },
  { path: '/', redirect: '/passport/login' },
  {
    path: '/passport',
    name: 'login',
    component: () => import('@/layout/passport/index.vue'),
    redirect: PageEnum.LOGIN_PATH,
    children: [
      {
        path: 'login',
        name: 'login',
        component: () => import('@/views/passport/login.vue'),
        meta: {
          requiresAuth: false,
          title: '登录'
        }
      }
    ]
  }
]
/* 主入口 */
const mainRoute: RouteRecordRaw[] = [...appRoutes]

const routes: RouteRecordRaw[] = [...globalRoute, ...mainRoute]
export const router = createRouter({
  // history: createWebHashHistory(),
  history: createWebHistory(),
  strict: true,
  scrollBehavior: () => ({ left: 0, top: 0 }),
  routes
})

// config router
export function setupRouter(app: App<Element>) {
  app.use(router)
}
