import {defineStore} from "pinia";
import type {IAppMenuList, IAppMinorMenuList} from "@/interface/app/IAppMenuList.ts";
import {computed, reactive, ref} from "vue";
import type {IUserMenuPermission} from "@/interface/page/user/IPermissionResponse.ts";

export const menuStore = defineStore('menu',
    () => {
        let appMenu = ref<IAppMenuList[]>([]);
        let userMenu = ref<IUserMenuPermission[]>([]);
        const returnMenu = computed(() => appMenu);
        let selectMenu = ref<string>('');
        function clearMenu(): void {
            appMenu.value = [];
        }
        function setMenu(menu: IAppMenuList[]): void {
            appMenu.value = menu;
        }
        function getMenu(): IAppMenuList[] {
            return appMenu.value;
        }
        function resetSelectMenu(): void {
            selectMenu.value = '';
        }
        function setSelectedMenu(menu: string): void {
            selectMenu.value = menu;
        }
        function setUserMenu(needUserMenu: IUserMenuPermission[]): void {
            userMenu.value = needUserMenu;
        }
        function getUserMenuWithRouterName(routerName: string | undefined): IUserMenuPermission| undefined {
            return userMenu.value.find(item => item.routerName == routerName);
        }
        function getUserMenuWithId(menuId: string | undefined): IUserMenuPermission| undefined {
            return userMenu.value.find(item => item.menuId == menuId);
        }
        return {appMenu, returnMenu, selectMenu, clearMenu, setMenu, getMenu, resetSelectMenu, setSelectedMenu,setUserMenu, getUserMenuWithRouterName,getUserMenuWithId};
    }, {
        persist: {
            key: 'menu',
        }
    })