import { setupLayouts } from 'virtual:meta-layouts'
import { createRouter, createWebHistory } from 'vue-router'
import { routes as fileRoutes } from 'vue-router/auto-routes'
import { safeResolve } from '~/composables/path'
declare module 'vue-router' {}
// 重定向 BASE_URL
fileRoutes.flat(Infinity).forEach((route) => {
	route.path = safeResolve(route.path)
})
export const router = createRouter({
	history: createWebHistory(),
	routes: setupLayouts(fileRoutes),
})
// 路由拦截
router.beforeEach((to, from, next) => {
	// 需要排除的路径
	const noAuthPaths = ['/login', '/register'];

	// 如果当前访问路径在排除列表中，则直接放行
	if (noAuthPaths.includes(to.path)) {
		return next();
	}

	// 进行管理员认证
	console.log("管理员认证~");
	console.log(userStore().isLogin);

	// 判断有没有登录
	if (!userStore().isLogin) {
		ElMessage.warning("认证失败~");
		return next('/login');
	}

	next();
});


export default router
