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

export const useAppStore = defineStore<"app", any, any, any>("app", {
  persist: {
    storage: sessionStorage,
    paths: ["serverStatus", "timeDiff"],
  },
  state: () => ({
    serverStatus: null,
    loadingStatus: {
      spinning: false,
      tip: "",
    },
    timeDiff: 0,
  }),
  actions: {
    setServerStatus(obj: any) {
      this.serverStatus = obj;
      this.timeDiff = obj ? obj.time - Date.now() : 0;
    },
    setState(data: any) {
      this.$patch(data);
    },
    setLoading(spinning: boolean, tip = "") {
      this.loadingStatus = {
        spinning,
        tip,
      };
    },
  },
});
