import { defineStore } from 'pinia'
import pinia from '~src/store/init'
import { clearStoreAll, getStoreValue, setStoreValue } from '~src/utils/storage'
import { removeToken } from '~src/api/util/auth'

export const useAppStore = defineStore('main', {
    state: () => ({
        userName: getStoreValue('userName') || '',
        projectId: getStoreValue('projectId') || '',
        isEchartSize: '',
    }),
    getters: {
        getUserName: state => state.userName,
        getProjectId: state => state.projectId,
    },
    actions: {
        setUserName(value) {
            this.userName = value
            setStoreValue('userName', value)
        },
        setProjectId(value) {
            this.projectId = value
            setStoreValue('projectId', value)
        },
        //清除缓存和token
        clearStoreData() {
            //清除状态
            this.userName = null
            this.projectId = ''
            this.isEchartSize = ''
            //清除缓存
            clearStoreAll()
            removeToken()
        },
    },
})

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