"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

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

var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));

var _objectWithoutPropertiesLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutPropertiesLoose"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _react = _interopRequireWildcard(require("react"));

var _dateUtils = require("../utils/dateUtils");

var _Calendar = _interopRequireWildcard(require("../Calendar/Calendar"));

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

var defaultProps = {
  as: _Calendar.default,
  calendarDate: [new Date(), (0, _dateUtils.addMonths)(new Date(), 1)],
  format: 'yyyy-MM-dd',
  index: 0,
  value: []
};

var Panel = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
  var Component = props.as,
      calendarDate = props.calendarDate,
      disabledDate = props.disabledDate,
      index = props.index,
      limitEndYear = props.limitEndYear,
      onChangeCalendarDate = props.onChangeCalendarDate,
      showOneCalendar = props.showOneCalendar,
      value = props.value,
      rest = (0, _objectWithoutPropertiesLoose2.default)(props, ["as", "calendarDate", "disabledDate", "index", "limitEndYear", "onChangeCalendarDate", "showOneCalendar", "value"]);

  var _useState = (0, _react.useState)(),
      calendarState = _useState[0],
      setCalendarState = _useState[1];

  var onMoveForward = (0, _react.useCallback)(function (nextPageDate) {
    onChangeCalendarDate === null || onChangeCalendarDate === void 0 ? void 0 : onChangeCalendarDate(index, nextPageDate);
  }, [index, onChangeCalendarDate]);
  var onMoveBackward = (0, _react.useCallback)(function (nextPageDate) {
    onChangeCalendarDate === null || onChangeCalendarDate === void 0 ? void 0 : onChangeCalendarDate(index, nextPageDate);
  }, [index, onChangeCalendarDate]);
  var handleChangePageDate = (0, _react.useCallback)(function (nextPageDate) {
    onChangeCalendarDate === null || onChangeCalendarDate === void 0 ? void 0 : onChangeCalendarDate(index, nextPageDate);
    setCalendarState(undefined);
  }, [index, onChangeCalendarDate]);
  var handleChangePageTime = (0, _react.useCallback)(function (nextPageDate) {
    onChangeCalendarDate(index, nextPageDate);
  }, [index, onChangeCalendarDate]);
  var toggleMonthDropdown = (0, _react.useCallback)(function () {
    setCalendarState(calendarState === _Calendar.CalendarState.DROP_MONTH ? undefined : _Calendar.CalendarState.DROP_MONTH);
  }, [calendarState]);
  var toggleTimeDropdown = (0, _react.useCallback)(function () {
    setCalendarState(calendarState === _Calendar.CalendarState.DROP_TIME ? undefined : _Calendar.CalendarState.DROP_TIME);
  }, [calendarState]);
  var inSameMonth = (0, _react.useCallback)(function (date) {
    return (0, _dateUtils.isSameMonth)(date, calendarDate[index]);
  }, [calendarDate, index]);
  var getPageDate = (0, _react.useCallback)(function () {
    return calendarDate[index];
  }, [calendarDate, index]);
  var handleMoveForward = (0, _react.useCallback)(function () {
    onMoveForward === null || onMoveForward === void 0 ? void 0 : onMoveForward((0, _dateUtils.addMonths)(getPageDate(), 1));
  }, [getPageDate, onMoveForward]);
  var handleMoveBackward = (0, _react.useCallback)(function () {
    onMoveBackward === null || onMoveBackward === void 0 ? void 0 : onMoveBackward((0, _dateUtils.addMonths)(getPageDate(), -1));
  }, [getPageDate, onMoveBackward]);
  var disabledBackward = (0, _react.useCallback)(function () {
    var after = (0, _dateUtils.isAfter)((0, _dateUtils.setDate)(calendarDate[1], 1), (0, _dateUtils.setDate)((0, _dateUtils.addMonths)(calendarDate[0], 1), 1));

    if (index === 0) {
      return false;
    }

    return !after;
  }, [calendarDate, index]);
  var disabledForward = (0, _react.useCallback)(function () {
    if (showOneCalendar) return false;
    var after = (0, _dateUtils.isAfter)((0, _dateUtils.setDate)(calendarDate[1], 1), (0, _dateUtils.setDate)((0, _dateUtils.addMonths)(calendarDate[0], 1), 1));

    if (index === 1) {
      return false;
    }

    return !after;
  }, [calendarDate, index, showOneCalendar]);
  var disabledMonth = (0, _react.useCallback)(function (date) {
    var after;

    if (disabledDate === null || disabledDate === void 0 ? void 0 : disabledDate(date, value, _utils.DATERANGE_DISABLED_TARGET.CALENDAR)) {
      return true;
    }

    if (showOneCalendar) return false;

    if (index === 1) {
      after = (0, _dateUtils.isAfter)(date, calendarDate[0]);
      return !after;
    }

    after = (0, _dateUtils.isAfter)(calendarDate[1], date);
    return !after;
  }, [calendarDate, disabledDate, index, showOneCalendar, value]);
  return /*#__PURE__*/_react.default.createElement(Component, (0, _extends2.default)({}, rest, {
    calendarState: calendarState,
    dateRange: value,
    disabledBackward: disabledBackward(),
    disabledDate: disabledMonth,
    disabledForward: disabledForward(),
    inSameMonth: inSameMonth,
    index: index,
    limitEndYear: limitEndYear,
    onChangePageDate: handleChangePageDate,
    onChangePageTime: handleChangePageTime,
    onMoveBackward: handleMoveBackward,
    onMoveForward: handleMoveForward,
    onToggleMonthDropdown: toggleMonthDropdown,
    onToggleTimeDropdown: toggleTimeDropdown,
    pageDate: getPageDate(),
    ref: ref
  }));
});

Panel.displayName = 'Panel';
Panel.defaultProps = defaultProps;
Panel.propTypes = {
  value: _propTypes.default.arrayOf(_propTypes.default.instanceOf(Date)),
  hoverValue: _propTypes.default.arrayOf(_propTypes.default.instanceOf(Date)),
  calendarDate: _propTypes.default.arrayOf(_propTypes.default.instanceOf(Date)),
  index: _propTypes.default.number,
  format: _propTypes.default.string,
  timeZone: _propTypes.default.string,
  isoWeek: _propTypes.default.bool,
  limitEndYear: _propTypes.default.number,
  classPrefix: _propTypes.default.string,
  disabledDate: _propTypes.default.func,
  onSelect: _propTypes.default.func,
  onMouseMove: _propTypes.default.func,
  onChangeCalendarDate: _propTypes.default.func
};
var _default = Panel;
exports.default = _default;