/*
 * 全局状态
 *
 * 使用 store 的名字，同时以 `use` 开头且以 `Store` 结尾
 *
 * 采用选项式语法：
 * ref() 就是 state 属性
 * computed() 就是 getters
 * function() 就是 actions
 */

import { defineStore } from "pinia";
import { ref, computed } from "vue";
import serviceAxios from "@/http/index";

export const useUserStore = defineStore(
  "user",
  () => {
    const userInfo = ref({
      token: "",
      user: {}, // 用户信息
    });

    const doubleCount = computed(() => {});

    /*
     * 设置用户信息
     */
    const setUserInfo = async (payload) => {
      userInfo.value.user = payload;
    };

    /*
     * 清空用户信息
     */
    const clearProfile = () => {
      userInfo.value.user = {};
      removeToken();
    };

    /*
     * 设置token
     */
    const setToken = (token) => {
      userInfo.value.token = token;
    };

    /*
     * 移除token
     */
    const removeToken = () => {
      userInfo.value.token = "";
    };

    // 检查用户是否登录
    const isUserLogin = () => {
      return userInfo.value.user && userInfo.value.token;
    };

    return {
      userInfo,
      doubleCount,
      setUserInfo,
      clearProfile,
      setToken,
      isUserLogin,
    };
  },
  {
    // 持久化配置
    persist: true,
  }
);
