import { createRouter, createWebHashHistory, Router } from "vue-router";
import Layout from "@/layout";
import { RouterTy } from "~/router";
import routesOne from "./routes/routes-file-one";
import routesTwo from "./routes/routes-file-two";
import routesThree from "./routes/routes-file-three";
import routesFour from "./routes/routes-file-four";
import routesFive from "./routes/routes-file-five";
import routesSix from "./routes/routes-file-six";
import routesSeven from "./routes/routes-file-seven";
import { Route, useKeepAlive } from "@/store/keepAlive";

const routesPush = (arr, routes) => {
  if (!Array.isArray(arr)) return "请传入数组格式路由表";
  for (let i = 0; i < arr.length; i++) {
    routes.push(arr[i]);
  }
};

export const constantRoutes: RouterTy = [
  {
    path: "/redirect",
    component: Layout,
    hidden: true,
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect"),
      },
    ],
  },
  {
    path: "/login",
    component: () => import("@/views/login/Login.vue"),
    hidden: true,
  },
  {
    path: "/404",
    component: () => import("@/views/error-page/404.vue"),
    hidden: true,
  },
  {
    path: "/401",
    component: () => import("@/views/error-page/401.vue"),
    hidden: true,
  },
];
/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes: RouterTy = [
  // 404 page must be placed at the end !!!
  // using pathMatch install of "*" in vue-router 4.0
  { path: "/:pathMatch(.*)", redirect: "/404", hidden: true },
];

// 插入路由
routesPush(
  [
    ...routesOne,
    ...routesSix,
    ...routesFive,
    ...routesTwo,
    ...routesThree,
    ...routesFour,
    ...routesSeven,
  ],
  asyncRoutes
);

const router: Router = createRouter({
  history: createWebHashHistory(),
  scrollBehavior: () => ({ top: 0 }),
  routes: constantRoutes,
});

router.beforeEach((to, from, next) => {
  const {
    savedRoutes,
    saveRoute,
    // cachedViews,
    addCachedViewAll,
    // delCachedView,
    delCachedViewAll,
    delCachedViewUntil,
  } = useKeepAlive();
  if (from.matched?.[0]?.name === "RealTime") {
    if (to.matched[1].name !== from.matched[1].name) {
      delCachedViewAll();
    } else if (to.matched.length < from.matched.length) {
      delCachedViewUntil(to.name as string);
    }

    if (
      typeof from.name === "string" &&
      ((from.name.includes("FlightView") &&
        from.query.flightNO &&
        from.query.flightDate) ||
        (from.name.includes("WaybillView") && from.query.waybillNO))
    ) {
      saveRoute(from as Route);
    }
  }

  if (to.matched?.[0]?.name === "RealTime") {
    if (to.meta.keepAlive) {
      addCachedViewAll(to.matched.map((route) => route.name) as string[]);
    }

    if (
      typeof to.name === "string" &&
      ((to.name.includes("FlightView") &&
        (!to.query.flightNO || !to.query.flightDate)) ||
        (to.name.includes("WaybillView") && !to.query.waybillNO))
    ) {
      const savedRoute = savedRoutes.find(
        (savedRoute) => savedRoute.name === to.name
      );
      if (savedRoute) {
        next(savedRoute.fullPath);
      }
    }
  }

  next();
});

// export function resetRouter() {
//   const newRouter = createRouter({
//     history: createWebHashHistory(),
//     scrollBehavior: () => ({ top: 0 }),
//     routes: constantRoutes
//   })
// }

export default router;
