// import { getToken } from "@/auth/auth";
// import { isNil } from "lodash-es";
import Vue from "vue";
import VueRouter from "vue-router";
// import Home from "../views/Home.vue";

// ignore NavigationDuplicated. https://github.com/vuejs/vue-router/issues/2881
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalPush.call(this, location, onResolve, onReject);
  try {
    return originalPush.call(this, location).catch(err => err);
  } catch (error) {
    console.log(error);
  }
};
// end ignore

Vue.use(VueRouter);

// function propsValidator(route, component) {
//   const props = { ...route.params };
//   Object.entries(props).map(([key, prop]) => {
//     console.log(prop, key);
//     if (!(prop instanceof component.props[key].type)) {
//       props[key] = component.props[key].type(prop);
//     }
//   });
//   return props;
// }
const sampleRoutes = [
  {
    path: "/samples/Login_01",
    name: "Login_01",
    component: () =>
      import(
        /* webpackChunkName: "about" */ "../samples/Login_01/Login_01.vue"
      ),
  },
  {
    path: "/samples/Layout_01",
    name: "Layout_01",
    component: () =>
      import(
        /* webpackChunkName: "about" */ "../samples/Layout_01/Layout_01.vue"
      ),
  },
];

const routes = [
  {
    path: "/",
    name: "Home",
    redirect: "/dashboard",
  },
  {
    path: "/dashboard",
    name: "Layout_01",
    component: () =>
      import(
        /* webpackChunkName: "about" */ "../features/Layout_01/Layout_01.vue"
      ),
    children: [
      {
        path: "sls-log",
        component: () =>
          import(
            /* webpackChunkName: "about" */ "../features/SlsLog/SlsLog.vue"
          ),
      },
    ],
  },
  // {
  //   path: "/dashboard/sls-log",
  //   name: "sls-log",
  //   component: () =>
  //     import(/* webpackChunkName: "about" */ "../features/SlsLog/SlsLog.vue"),
  // },
  {
    path: "/about",
    name: "About",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/About.vue"),
  },
  ...sampleRoutes,
  // {
  //   path: "/xxx/:id",
  //   component: XXX,
  //   props(route) {
  //     return propsValidator(route, XXX);
  //   }
  // }
  // {
  //   path: "/login",
  //   component: () =>
  //     import(/* webpackChunkName: "login" */ "../features/Login/Login.vue"),
  // },
  {
    path: "/*",
    component: () =>
      import(/* webpackChunkName: "default" */ "../views/404.vue"),
  },
];

const router = new VueRouter({
  mode: "hash",
  base: process.env.BASE_URL,
  routes,
});

// FIXME: router.route 添加 auth，代表是否需要认证。
// FIXME: roles. 根据roles来授权。
// FIXME: 在Login页面，验证通过后，返回redirectTo 页面。
router.beforeEach((to, from, next) => {
  if (to.path) {
    window._hmt.push(["_trackPageview", to.fullPath]);
  }
  next();

  // const token = getToken();
  // if (isNil(token) && to.path.includes("/login") === false) {
  //   router.push("/login?redirectTo=" + encodeURI(to.fullPath));
  //   next(false);
  // } else {
  //   next();
  // }
});

export default router;
