import { toRefs, shallowReadonly, ShallowRef } from 'vue'
import { queryAccountMenu } from '@/services/api/account'
import { VueStore } from '../base'
import WebStorage from '@/utils/storage/base'

interface StoreState {
    loading: boolean;
    userMenus: ShallowRef<Model.UserMenu[]>;
}

const store = new (class extends VueStore<StoreState>{
    constructor() {
        const storage = new WebStorage<Model.UserMenu[]>(sessionStorage, 'userMenus', [])
        const state: StoreState = {
            loading: false,
            userMenus: storage.getRef()
        }
        super(state)
    }

    actions = {
        /** 获取用户菜单列表 */
        getUserMenuList: () => {
            this.state.loading = true
            return queryAccountMenu({
                success: (res) => {
                    this.state.userMenus = res.data
                },
                complete: () => {
                    this.state.loading = false
                }
            })
        },
        /** 重置数据 */
        reset: () => {
            this.state.userMenus = []
        }
    }
})

export function useMenuStore() {
    return shallowReadonly({
        ...toRefs(store.state),
        ...store.actions,
        ...store.methods,
    })
}