import Vue from "vue";
import App from "./App.vue";
import VueRouter, { Route, RouteConfig } from "vue-router";
import { propsConvert } from "./services/props-convert";
import VueDc from "./index";
import { RestfulResponse } from "./model/RestfulResponse";
import { oauthService } from "./services/oauth.service";
import { Code } from "./constants/enum/code.enum";

Vue.config.productionTip = false;

Vue.use(VueDc, {
  sysId: "881201085290409985",
  baseApi: process.env.VUE_APP_MS_SYSTEM_API,
  oauth: {
    authorizeUrl: process.env.VUE_APP_MS_AUTH_API,
    clientId: process.env.VUE_APP_CLIENT_ID,
    clientSecret: process.env.VUE_APP_CLIENT_SECRET,
  },
});

Vue.use(VueRouter);
const routes: Array<RouteConfig> = [
  {
    path: "/",
    redirect: `/test-a`,
    props: propsConvert,
  },
  {
    path: `/test-a`,
    component: () => import(/* webpackChunkName: "TEST" */ "@/test/TestA.vue"),
    props: propsConvert,
    meta: { name: "测试A", keepAlive: true },
  },
  {
    path: `/test-b`,
    component: () => import(/* webpackChunkName: "TEST" */ "@/test/TestA.vue"),
    props: propsConvert,
    meta: { nameKey: "name", parent: "/test-a", keepAlive: true },
  },
  {
    path: `/test-c`,
    component: () => import(/* webpackChunkName: "TEST" */ "@/test/TestA.vue"),
    props: propsConvert,
    meta: { name: "测试C", parent: "/test-b", keepAlive: true },
  },
  {
    path: `/test-d`,
    component: () => import(/* webpackChunkName: "TEST" */ "@/test/TestA.vue"),
    props: propsConvert,
    meta: { name: "测试D", parent: "/test-c", keepAlive: true },
  },
  {
    path: `/test-e`,
    component: () => import(/* webpackChunkName: "TEST" */ "@/test/TestA.vue"),
    props: propsConvert,
    meta: { name: "测试E", keepAlive: true },
  },
  {
    path: `/test-f`,
    component: () => import(/* webpackChunkName: "TEST" */ "@/test/TestA.vue"),
    meta: { name: "测试F" },
  },
  {
    path: `/test-g`,
    component: () => import(/* webpackChunkName: "TEST" */ "@/test/TestA.vue"),
    meta: { name: "测试G" },
  },
  {
    path: `/test-h`,
    component: () => import(/* webpackChunkName: "TEST" */ "@/test/TestA.vue"),
    meta: { name: "测试H" },
  },
  {
    path: `/test-i`,
    component: () => import(/* webpackChunkName: "TEST" */ "@/test/TestA.vue"),
    meta: { name: "测试I" },
  },
  {
    path: `/test-j`,
    component: () => import(/* webpackChunkName: "TEST" */ "@/test/TestA.vue"),
    meta: { name: "测试J" },
  },
  {
    path: `/test-k`,
    component: () => import(/* webpackChunkName: "TEST" */ "@/test/TestA.vue"),
    meta: { name: "测试K" },
  },
  {
    path: `/test-l`,
    component: () => import(/* webpackChunkName: "TEST" */ "@/test/TestA.vue"),
    meta: { name: "测试L" },
  },
  {
    path: `/test-m`,
    component: () => import(/* webpackChunkName: "TEST" */ "@/test/TestA.vue"),
    meta: { name: "测试M" },
  },
  {
    path: `/test-n`,
    component: () => import(/* webpackChunkName: "TEST" */ "@/test/TestA.vue"),
    meta: { name: "测试N" },
  },
  {
    path: `/test-o`,
    component: () => import(/* webpackChunkName: "TEST" */ "@/test/TestA.vue"),
    meta: { name: "测试O" },
  },
  {
    path: `/test-p`,
    component: () => import(/* webpackChunkName: "TEST" */ "@/test/TestA.vue"),
    meta: { name: "测试P" },
  },
];

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

router.beforeEach((to: Route, from: Route, next: () => any) => {
  router.app.$routeTopologyService.checkAndInit(router);
  router.app.$oauthService.checkAndLogin().then((res: RestfulResponse) => {
    if (res.code === Code.SUCCESS.code) {
      oauthService.isLogin = true;
    } else {
      console.log(res);
    }
    next();
  });
});

new Vue({
  router,
  render: (h) => h(App),
}).$mount("#app");
