import { defineStore } from "pinia";
import { ref, watch } from "vue";
import { cache } from "@/utils/cache";

export const useUserStore = defineStore("user", () => {
  const userInfo = ref(cache('USERINFO_KEY') ? JSON.parse(cache('USERINFO_KEY')) : {
    username: "",
  });
  
const adressInfo = ref(cache('ADRESS_KEY') ? JSON.parse(cache('ADRESS_KEY')) : {
    latitude: "",
    longitude: "",
    adress: "",
  });
  
watch(userInfo, (newValue) => cache('USERINFO_KEY', newValue ? JSON.stringify(newValue) : null));
watch(adressInfo, (newValue) => cache('ADRESS_KEY', newValue ? JSON.stringify(newValue) : null));
  
function setUserInfo(payload) {
    userInfo.value = payload;
}
function setAdressInfo(payload) {
    adressInfo.value = payload;
}

  return {
    userInfo,
	setUserInfo,
	adressInfo,
	setAdressInfo
  };
});
