import { defineStore } from "pinia";

export const useUserStore = defineStore("user", {
    state: () => ({
        userName: "",
        userAvatar: "",
        isLoggedIn: false,
    }),
    actions: {
        async fetchUserInfo() {
            const { data, error } = await useApiFetch("/user/detail")
                .get()
                .json();
            if (data.value && !error.value) {
                if (data.value.code === 200) {
                    const userinfo = data.value.data.userInfo;
                    this.userName = userinfo.nickName || "";
                    this.userAvatar = userinfo.userAvatar;
                    this.isLoggedIn = true;
                }
            }
        },
    },
});
