/*
 * @Author: donghao donghao@supervision.ltd
 * @Date: 2025-03-06 13:53:22
 * @LastEditors: donghao donghao@supervision.ltd
 * @LastEditTime: 2025-03-10 09:59:29
 * @FilePath: \vite-ai\data-dashboard\src\router\index.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { createRouter, createWebHistory } from "vue-router";
import { useUserStore } from "@/stores/user";
import { dashboardRoutes } from "./dashboard";
import { buildHierarchyTree } from "@/utils/tree";

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: "/login",
      fullPath: "/login",
      component: () => import("@/views/login/Login.vue"),
      meta: {
        keepAlive: false,
        requiresAuth: false,
        isDashboard: true,
      },
    },
    {
      path: "/",
      redirect: "/dashboard",
    },
    { ...dashboardRoutes },
  ],
});

router.beforeEach((to) => {
  const userStore = useUserStore();

  if (to.meta.requiresAuth && !userStore.token) {
    return "/login";
  }

  //   if (to.path === "/login" && userStore.token) {
  //     return "/dashboard";
  //   }
});

/** 重置路由 */
export function resetRouter() {
  router.getRoutes().forEach(route => {
    const { name, meta } = route;
    if (name && router.hasRoute(name) && meta?.backstage) {
      router.removeRoute(name);
    }
  });
}

export default router;
