import _extends from "@babel/runtime/helpers/esm/extends";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
import PropTypes from 'prop-types';
import React, { useCallback, useState } from 'react';
import { addMonths, isAfter, isSameMonth, setDate } from '../utils/dateUtils';
import CalendarCore, { CalendarState } from '../Calendar/Calendar';
import { DATERANGE_DISABLED_TARGET } from '../utils';
var defaultProps = {
  as: CalendarCore,
  calendarDate: [new Date(), addMonths(new Date(), 1)],
  format: 'yyyy-MM-dd',
  index: 0,
  value: []
};
var Calendar = /*#__PURE__*/React.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 = _objectWithoutPropertiesLoose(props, ["as", "calendarDate", "disabledDate", "index", "limitEndYear", "onChangeCalendarDate", "showOneCalendar", "value"]);

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

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

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

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

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

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

    if (disabledDate !== null && disabledDate !== void 0 && disabledDate(date, value, DATERANGE_DISABLED_TARGET.CALENDAR)) {
      return true;
    }

    if (showOneCalendar) return false;

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

    after = isAfter(calendarDate[1], date);
    return !after;
  }, [calendarDate, disabledDate, index, showOneCalendar, value]);
  return /*#__PURE__*/React.createElement(Component, _extends({}, 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,
    calendarDate: getCalendarDate(),
    ref: ref
  }));
});
Calendar.displayName = 'DateRangePicker.Calendar';
Calendar.defaultProps = defaultProps;
Calendar.propTypes = {
  value: PropTypes.arrayOf(PropTypes.instanceOf(Date)),
  hoverValue: PropTypes.arrayOf(PropTypes.instanceOf(Date)),
  calendarDate: PropTypes.arrayOf(PropTypes.instanceOf(Date)),
  index: PropTypes.number,
  format: PropTypes.string,
  isoWeek: PropTypes.bool,
  limitEndYear: PropTypes.number,
  classPrefix: PropTypes.string,
  disabledDate: PropTypes.func,
  onSelect: PropTypes.func,
  onMouseMove: PropTypes.func,
  onChangeCalendarDate: PropTypes.func
};
export default Calendar;