import { defineStore } from 'pinia'
import pinia from '~src/store/init'
import { clearStoreAll, getStore, setStore } from '~src/utils/storage'

export const useAppStore = defineStore('main', {
    state: () => ({
        //用户信息
        userInfo: getStore('userInfo') || {},
        //菜单信息
        menus: getStore('menus') || [],
        home: getStore('home') || 'anomaly-invest',
        buttons: getStore('buttons') || {},
    }),
    getters: {
        getUserInfo: state => state.userInfo,
        getMenus: state => state.menus,
        getHome: state => state.home,
        getButtons: state => state.buttons,
    },
    actions: {
        //用户信息
        setUserInfo(value) {
            this.userInfo = value
            setStore('userInfo', value)
        },
        //菜单信息
        setMenus(value) {
            this.menus = value
            setStore('menus', value)
        },
        setButtons(value) {
            this.buttons = value
            setStore('buttons', value)
        },
        setHomePage(value) {
            this.home = value
            setStore('home', value)
        },
        getButtonsVal(value) {
            return this.buttons[value] || false
        },
        //清除缓存和token
        clearStoreData() {
            this.userInfo = null
            this.menus = null
            this.buttons = null
            //清除缓存
            clearStoreAll()
        },
    },
})

export default function useUserStoreWidthOut() {
    return useAppStore(pinia)
}
