import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
// 引入 nprogress 相关方法
import { close, start } from '@/utils/nprogress/nprogress'
import login from './modules/login'

const routes: Array<RouteRecordRaw> = [...login]

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

// 路由前置后卫
router.beforeEach(() => {
	// 开启进度条
	start()
})
// 路由后置后卫
router.afterEach(() => {
	// 关闭进度条
	close()
})

/* 初始化路由表 */
export function resetRouter() {
	router.getRoutes().forEach((route) => {
		const { name } = route
		if (name) {
			router.hasRoute(name) && router.removeRoute(name)
		}
	})
}

export default router
