import { defineStore } from "pinia";
import { TokenInfo, UserStore } from "@/types/stores/userStore.ts";

export const useUserStore = defineStore({
    id: 'useUserStore',
    state: (): UserStore => {
        return {
            tokenInfo: undefined
        }
    },
    actions: {
        saveTokenInfo(tokenInfo: TokenInfo) {
            this.tokenInfo = tokenInfo
        },
        async resetUserInfo() {
            this.tokenInfo = undefined;
        }
    },
    getters: {
        getTokenInfo: (state): TokenInfo => state.tokenInfo as TokenInfo,
    },

    // persist 是 Pinia 的一个插件 pinia-plugin-persistedstate 提供的功能
    // 用来让状态数据在页面刷新或重新加载时保存在浏览器的存储中
    // 通过这个插件，你可以让 Pinia store 的状态持久化，即即使页面刷新，数据也不会丢失
    persist: {
        key: "useUserStore",
        storage: window.localStorage,
        paths: ["tokenInfo"],
    }
})
