import { useMemoizedFn } from "ahooks";
import { useMemo } from "react";
import { dayjs } from "../../utils/dayjs.mjs";
const useDateTimePicker = ({
  value,
  onChange,
  startDate = dayjs().subtract(10, "year").toDate(),
  endDate = dayjs().add(10, "year").toDate(),
  utcOffset = dayjs().utcOffset(),
  format,
  formatter
}) => {
  const convertToLocal = useMemoizedFn((date) => {
    const targetTime = dayjs(date).utcOffset(utcOffset);
    const converted = targetTime.utcOffset(dayjs().utcOffset(), true);
    return new Date(converted.format());
  });
  const convertFromLocal = useMemoizedFn((date) => {
    const targetTime = dayjs(date).utcOffset(utcOffset, true);
    return new Date(targetTime.format());
  });
  const displayValue = useMemo(() => {
    if (!value) return void 0;
    return convertToLocal(value);
  }, [value, convertToLocal]);
  const displayStartDate = useMemo(() => {
    return convertToLocal(startDate);
  }, [startDate, convertToLocal]);
  const displayEndDate = useMemo(() => {
    return convertToLocal(endDate);
  }, [endDate, convertToLocal]);
  const handleChange = useMemoizedFn((localTime) => {
    const targetTime = convertFromLocal(localTime);
    onChange == null ? void 0 : onChange(targetTime);
  });
  return {
    value: displayValue,
    onChange: handleChange,
    startDate: displayStartDate,
    endDate: displayEndDate,
    formatter: (val) => {
      const targetTime = dayjs(val).utcOffset(utcOffset, true);
      return formatter ? formatter(new Date(targetTime.format())) : targetTime.format(format);
    }
  };
};
export {
  useDateTimePicker
};
