import {reactive, ref} from 'vue'
import { defineStore } from 'pinia'
import type {IThisUserInfo} from "@/interface/app/IThisUserInfo.ts";
import type {IMenuPermission} from "@/interface/app/IMenuPermission.ts";

export const userStore = defineStore('user', () => {
    let thisLoginInfo = ref<IThisUserInfo>({ });

    let menuPermission = ref<IMenuPermission[]>([])

    // 设置用户信息
    function getUserInfo(): IThisUserInfo {
        return thisLoginInfo.value;
    }
    // 获取用户信息
    function setUserInfo(user: IThisUserInfo): void {
        thisLoginInfo.value = user;
    }

    // 清除菜单信息
    function clearMenuPermission(): void {
        menuPermission.value = [];
    }
    // 设置菜单信息
    function setMenuPermission(menuPermissions: IMenuPermission[]): void {
        menuPermission.value = menuPermissions;
    }
    // 获取菜单信息
    function getMenuPermissions(): IMenuPermission[] {
        return menuPermission.value;
    }
    // 判断路由名称是否存在
    function checkMenuPermission(menuRouterName: string): boolean {
        if(!menuRouterName) return false;
        if(!menuPermission || menuPermission.value.length < 1) return false;
        const menu = menuPermission.value.find((item: IMenuPermission) => item.routerName == menuRouterName);
        return menu != undefined;
    }

    return {thisLoginInfo, menuPermission, getUserInfo, setUserInfo, clearMenuPermission, setMenuPermission, checkMenuPermission, getMenuPermissions};

}, {
    persist: {
        key: 'user',
    }
})