import jalaali from "jalaali-js";
import { useEffect, useState } from "react";
import type { CalendarDate } from "~/types/util.type";

const { jy } = jalaali.toJalaali(new Date());

export function useStoredDate(): [
  CalendarDate,
  React.Dispatch<React.SetStateAction<CalendarDate>>,
] {
  const [date, setDate] = useState<CalendarDate>({});

  useEffect(() => {
    const storedDate = localStorage.getItem("dateSelected");

    if (storedDate) {
      setDate(JSON.parse(storedDate));
    } else {
      setDate({
        start: `${jy}/01/01`,
        end: `${jy}/12/30`,
      });
    }
  }, [jy]);

  return [date, setDate];
}
