import { React } from "../deps.ts";

const VOCA_SETTING_KEY = "voca-setting";

export default function useDownloadFormState() {
  const [levels, setLevels] = React.useState<number[]>([6, 7]);
  const [day, setDay] = React.useState<number>(1);

  React.useEffect(() => {
    const setting = JSON.parse(localStorage.getItem(VOCA_SETTING_KEY) ?? "{}");
    setting.level && setLevels(setting.levels);
    setting.day && setDay(setting.day);
  }, []);

  const save = React.useCallback(({ levels, day }) => {
    localStorage.setItem(VOCA_SETTING_KEY, JSON.stringify({ levels, day }));
  }, []);

  return {
    levelState: [levels, setLevels] as const,
    dayState: [day, setDay] as const,
    save,
  };
}
