import { watch } from "vue";

export const getLocal = <T>(key: string, def: T): T => {
  let result: T = def;
  try {
    const str = localStorage.getItem(key);
    if (str) {
      result = JSON.parse(str);
    }
  } catch {
    result = def;
  }
  return result;
};

export const changSaveLocal = <T>(key: string, get: () => T) => {
  watch(get, () => localStorage.setItem(key, JSON.stringify(get())), {
    deep: true,
  });
};
