import {defineStore} from "pinia";
import {reactive, ref} from "vue";
import router from "../../router/index.js";
import generateRouter from "../../router/generateRouter.js";
import Api from "../../api/index.js";

export const useSystemStore = defineStore("SystemStore", () => {
    const isRoot = ref(false);
    const RoutesTemp = ref([]);
    const NOW_ROUTER = ref(null);
    const NOW_ROUTER_QUERY = reactive({});
    const BUILD_MODE = ref(import.meta.env.VITE_BUILD_MODE);

    const installRoute = async () => {
        const routes = generateRouter(RoutesTemp.value);
        router.removeRoute('home');
        router.addRoute({
            path: '/home',
            name: 'home',
            component: () => import('../../pages/layout/index.vue'),
            redirect: `/home/${routes[0].path}`,
            children: routes
        });
        router.addRoute({
            path: '/manage-materials',
            name: 'manage-materials',
            component: () => import('../../pages/manage-materials/manage-materials.vue'),
        });
        await router.replace(router.currentRoute.value.fullPath);
    }

    const setRouter = async (_isRoot) => {
        isRoot.value = _isRoot;

        RoutesTemp.value.length = 0;

        // 请求资源 mockRoutes
        if (isRoot.value) { // root
            const {data} = await Api.admin.getMenu();
            RoutesTemp.value.push(...data);
        } else {
            const {data} = await Api.merchant.getMenu();
            RoutesTemp.value.push(...data);
        }

        await installRoute();
    }

    const clearRouter = async () => {
        const routes = generateRouter(RoutesTemp.value);
        RoutesTemp.value.length = 0;
        router.removeRoute('home');
        router.addRoute({
            path: '/home',
            name: 'home',
            component: () => import('../../pages/layout/index.vue'),
            redirect: `/home/${routes[0].path}`,
            children: []
        });
        router.addRoute({
            path: '/manage-materials',
            name: 'manage-materials',
            component: () => import('../../pages/manage-materials/manage-materials.vue'),
        });
        isRoot.value = false;
        await router.replace(router.currentRoute.value.fullPath);
    }

    const updateRouterTag = async () => {
        let routerList;
        if (isRoot.value) {
            const {data} = await Api.admin.getMenu();
            routerList = data;
        } else {
            const {data} = await Api.merchant.getMenu();
            routerList = data;
        }
        RoutesTemp.value = routerList;
    }

    return {
        isRoot,
        RoutesTemp,
        NOW_ROUTER,
        NOW_ROUTER_QUERY,
        setRouter,
        installRoute,
        clearRouter,
        updateRouterTag,
        BUILD_MODE,
    }
}, {
    persist: {
        key: 'SystemStore',
        storage: localStorage,
        afterHydrate: (val) => {
            val.store.installRoute && val.store.installRoute();
        },
        pick: ['RoutesTemp', 'NOW_ROUTER', 'NOW_ROUTER_QUERY', 'isRoot'],
    }
});
