import { getUserCenter } from "@/api/user";
import { TOKEN_KEY } from "@/enums/constantEnums";
import { useSharedId } from "@/hooks/useShareMessage";
import { getToken } from "@/utils/auth";
import cache from "@/utils/cache";
import { defineStore } from "pinia";

interface UserSate {
  userInfo: Record<string, any>;
  token: string | null;
  client: string | null;
  temToken: string | null;
}
export const useUserStore = defineStore({
  id: "userStore",
  state: (): UserSate => ({
    userInfo: {},
    token: getToken() || null,
    client: null,
    temToken: null,
  }),
  getters: {
    isLogin: (state) => !!state.token,
  },
  actions: {
    async getUser() {
      const data = await getUserCenter({
        token: this.token,
      });
      this.userInfo = data;
    },
    login(token: string) {
      cache.set(TOKEN_KEY, token);
      this.token = token;
      useSharedId();
    },
    logout() {
      this.token = "";
      this.userInfo = {};
      cache.remove(TOKEN_KEY);
    },
  },
});
