"use strict";

exports.__esModule = true;
exports.default = void 0;

var _react = require("react");

var _utils = require("../utils");

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

  var valueRef = (0, _react.useRef)(value);

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

  var setCalendarDate = (0, _react.useCallback)(function (date) {
    if (date.valueOf() !== calendarDate.valueOf()) {
      setValue(date);
    }
  }, [calendarDate]);
  (0, _utils.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
  };
};

var _default = useCalendarDate;
exports.default = _default;