import { createRouter, createWebHistory } from 'vue-router';
import { createRouterGuards } from './guards';
import { useAuthStore } from '@/stores/modules/auth';
import { staticRoute, errorRouter } from './modules/staticRouter';

/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHistory(),
  routes: [...staticRoute, ...errorRouter],
  strict: true,
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

export const resetRouter = () => {
  const authStore = useAuthStore();
  authStore.flatMenuListGet.forEach((route) => {
    const { name } = route;
    if (name && router.hasRoute(name)) router.removeRoute(name);
  });
};

/**
 * 安装路由
 */
export function setupRouter(app) {
  app.use(router);
  // 创建路由守卫
  createRouterGuards(router);
}

export default router;
