import { toJalali as toJalaali, toGregorian } from "date-fns-jalali/_jalali";

export const toJalali = (date) => {
  const gregorianDate = date instanceof Date ? date : new Date(date);
  const jalali = toJalaali(
    gregorianDate.getFullYear(),
    gregorianDate.getMonth() + 1,
    gregorianDate.getDate()
  );
  return {
    jy: jalali.jy,
    jm: jalali.jm - 1,
    jd: jalali.jd,
  };
};

export const fromJalali = (jy, jm, jd) => {
  const month = jm >= 1 && jm <= 12 ? jm : jm + 1;
  const gregorian = toGregorian(jy, month, jd);
  return new Date(gregorian.gy, gregorian.gm - 1, gregorian.gd);
};
