import { ref, reactive } from "vue";
import { defineStore } from "pinia";

export const userInfoStore = defineStore(
  "userInfo",
  () => {
    const token = ref(null);

    const user = reactive({
      id: "",
      name: "",
      profile: "@/assets/defaultavatar.jpg",
      account: "",
      role: "",
    });

    const clearUserInfo = () => {
      token.value = null;

      user.id = "";
      user.name = "";
      user.profile = "@/assets/defaultavatar.jpg";
      user.account = "";
      user.role = "";

      // 清除本地存储的用户信息
      localStorage.removeItem("userInfo");
    };

    return {
      token,
      user,
      clearUserInfo,
    };
  },
  {
    persist: {
      key: "userInfo",
      storage: localStorage,
      paths: ["token", "user"],
    },
  }
);

export const messagePointStore = defineStore("message", () => {
  const hasNewMessage = ref(false);

  return {
    hasNewMessage,
  };
});
