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

export const useAppStore = defineStore('main', {
    state: () => ({
        projectInfo: getStore('projectInfo') ?? {},
        projectData: getStore('projectData') ?? [],
        dataPath: getStore('dataPath') ?? '', //数据存储路径
    }),
    actions: {
        setProjectInfo(value) {
            this.projectInfo = value
            setStore('projectInfo', value)
        },
        setProjectData(value) {
            this.projectData = value
            setStore('projectData', value)
        },
        setDataPath(value) {
            this.dataPath = value
            setStore('dataPath', value)
        },
        //清除缓存
        clearStoreData() {
            this.projectInfo = {}
            this.projectData = []
            this.dataPath = null
            delStore('projectInfo')
            delStore('projectData')
            delStore('dataPath')
        },
    },
})

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