import { useState, useCallback, useRef } from 'react';
import { useUpdateEffect } from '../utils';

var useCalendarDate = function useCalendarDate(value, defaultDate) {
  var _ref;

  var valueRef = useRef(value);

  var _useState = useState((_ref = value !== null && value !== void 0 ? value : defaultDate) !== null && _ref !== void 0 ? _ref : new Date()),
      calendarDate = _useState[0],
      setValue = _useState[1];

  var setCalendarDate = useCallback(function (date) {
    if (date.valueOf() !== calendarDate.valueOf()) {
      setValue(date);
    }
  }, [calendarDate]);
  useUpdateEffect(function () {
    var _valueRef$current;

    if ((value === null || value === void 0 ? void 0 : value.valueOf()) !== ((_valueRef$current = valueRef.current) === null || _valueRef$current === void 0 ? void 0 : _valueRef$current.valueOf())) {
      setCalendarDate(value);
      valueRef.current = value;
    }
  }, [value]);
  return {
    calendarDate: calendarDate,
    setCalendarDate: setCalendarDate
  };
};

export default useCalendarDate;