import { createApp } from "vue";
import router from "@/router";
import { NavigationGuardNext, RouteLocationNormalized } from "vue-router";

import { axios, Network, userStore, VDraggable, VPopup } from "timi-web";
import { DialogPlugin } from "tdesign-vue-next";

import "tdesign-vue-next/dist/tdesign.css";
import "timi-tdesign-pc/style.css";
import "timi-web/style.css";

import Root from "@/Root.vue";

console.log(`
      ______                    __ _ _
     /    __\\                   \\ \\ \\ \\
    / . . \\ '                    \\ \\ \\ \\
   (       )      imyeyu.com      ) ) ) )
   '\\ ___ /'                     / / / /
====='===='=====================/_/_/_/
`);

// ---------- 网络 ----------
axios.defaults.baseURL = import.meta.env.VITE_API;
axios.interceptors.request.use(Network.userTokenInterceptors);
// ---------- 路由 ----------
router.beforeEach(async (to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext): Promise<void> => {
	await userStore.login4Storage();
	if (to.matched.length !== 0) {
		next();
		return;
	}
	if (from.name) {
		next({name: from.name});
		return;
	}
	// 找不到页面
	const alert = DialogPlugin.alert({
		header: "错误：404",
		body: "找不到该页面",
		confirmBtn: {
			content: "关闭",
			variant: "base",
			theme: "default"
		},
		onConfirm: () => {
			alert.destroy();
			next("/");
		}
	});
});
// ---------- Vue ----------
const app = createApp(Root);
app.use(router);
app.directive("draggable", VDraggable as any);
app.directive("popup", VPopup as any);
app.mount("#root");
