import { defineStore } from "pinia";
import { userType } from "@/types/user";

interface user {
  userInfor: userType;
  token: string | undefined;
  refreshToken: string | undefined;
}

export const userInfor = defineStore("userInfor", {
  state(): user {
    return {
      userInfor: {
        name: "",
      },
      token: "",
      refreshToken: "",
    };
  },
  actions: {
    setLoginState(userData: userType) {
      this.userInfor = userData;
      localStorage.setItem("userInfor", JSON.stringify(userData));
    },
    removeLoginState() {
      localStorage.removeItem("userInfor");
    },
    setToken(token: string) {
      this.token = token;
      localStorage.setItem("token", token);
    },
    setRefToken(toekn: string) {
      this.refreshToken = toekn;
      localStorage.setItem("refreshToken", toekn);
    },
    removeToken() {
      this.token = undefined;
      this.refreshToken = undefined;
      localStorage.removeItem("token");
      localStorage.removeItem("refreshToken");
      localStorage.removeItem("historyRouterPath");
    },
  },
});
