"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const ahooks = require("ahooks");
const React = require("react");
const dayjs = require("../../utils/dayjs.cjs");
const useDateTimePicker = ({
  value,
  onChange,
  startDate = dayjs.dayjs().subtract(10, "year").toDate(),
  endDate = dayjs.dayjs().add(10, "year").toDate(),
  utcOffset = dayjs.dayjs().utcOffset(),
  format,
  formatter
}) => {
  const convertToLocal = ahooks.useMemoizedFn((date) => {
    const targetTime = dayjs.dayjs(date).utcOffset(utcOffset);
    const converted = targetTime.utcOffset(dayjs.dayjs().utcOffset(), true);
    return new Date(converted.format());
  });
  const convertFromLocal = ahooks.useMemoizedFn((date) => {
    const targetTime = dayjs.dayjs(date).utcOffset(utcOffset, true);
    return new Date(targetTime.format());
  });
  const displayValue = React.useMemo(() => {
    if (!value) return void 0;
    return convertToLocal(value);
  }, [value, convertToLocal]);
  const displayStartDate = React.useMemo(() => {
    return convertToLocal(startDate);
  }, [startDate, convertToLocal]);
  const displayEndDate = React.useMemo(() => {
    return convertToLocal(endDate);
  }, [endDate, convertToLocal]);
  const handleChange = ahooks.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.dayjs(val).utcOffset(utcOffset, true);
      return formatter ? formatter(new Date(targetTime.format())) : targetTime.format(format);
    }
  };
};
exports.useDateTimePicker = useDateTimePicker;
