import { createApp } from "vue";
import App from "./App.vue";
import VueDc, { AuthType, Code, propsConvert, RestfulResponse } from "./index";
import { createRouter, createWebHistory, NavigationGuardNext, RouteLocationNormalized, RouteRecordRaw } from "vue-router";
import { Vue } from "vue-class-component";
import { message } from "ant-design-vue";

Vue.registerHooks(["beforeRouteEnter", "beforeRouteLeave", "beforeRouteUpdate"]);

const app: any = createApp(App);

const routes: Array<RouteRecordRaw> = [
  {
    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 = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
});

router.beforeEach((to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext) => {
  const ins = app.config.globalProperties;
  ins.$routeTopologyService.checkAndInit(router);
  ins.$oauthService.checkAndLogin().then((res: RestfulResponse) => {
    if (res.code === Code.SUCCESS.code) {
      ins.$oauthService.isLogin.value = true;
      next();
    } else {
      message.success({ duration: 1, content: res.msg! });
    }
  });
});
const dcOptions = {
  sysId: "881201085290409985",
  baseApi: process.env.VUE_APP_MS_SYSTEM_API,
  authPreferTo: AuthType.OAUTH.code,
  oauth: {
    authorizeUrl: process.env.VUE_APP_MS_AUTH_API,
    clientId: process.env.VUE_APP_CLIENT_ID,
    clientSecret: process.env.VUE_APP_CLIENT_SECRET,
    enableNoticeMode: true,
    simpleLoginUri: "/api/v1/sys-user-info/permit_endpoint/login",
  },
  wxQyOauth: {
    enable: false,
    state: "wx_qy_oauth",
  },
  cas: {
    authorizeUrl: "https://www.baidu.com/",
    redirectUriKey: "service",
    voucherKey: "ticket",
    innerLoginUri: "/sys-user-info/permit_endpoint/casLogin",
  },
};
app.use(router).use(VueDc, dcOptions).mount("#app");
