import { onMounted, ref, watch } from "vue";

export function useStorageVar<T>(key: string, defaultValue: T) {
  const value = ref<T>(defaultValue);

  watch(value, (newValue) => {
    uni.setStorageSync(key, JSON.stringify(newValue));
  })

  onMounted(() => {
    const storageValue = uni.getStorageSync(key);
    if (storageValue) {
      value.value = JSON.parse(storageValue);
    }
  })

  return {
    value,
  }
}