'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _rollupPluginBabelHelpers = require('../../../_virtual/_rollupPluginBabelHelpers.js');
var React = require('react');
var calendarRangeHelper = require('../utils/calendarRangeHelper.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var React__default = /*#__PURE__*/_interopDefault(React);

var _excluded = ["value", "min", "max", "disabledList", "eventList", "eventMonthList", "disabledMonthList", "eventYearList", "disabledYearList", "onChangeValue", "onChangeStartOfRange", "onChangeSingleValue"];

/**
 * HOC для календаря, дающий возможность выбора диапазона даты
 */
var withRange = function withRange(Component) {
  return function (_ref) {
    var value = _ref.value,
      min = _ref.min,
      max = _ref.max,
      disabledList = _ref.disabledList,
      eventList = _ref.eventList,
      eventMonthList = _ref.eventMonthList,
      disabledMonthList = _ref.disabledMonthList,
      eventYearList = _ref.eventYearList,
      disabledYearList = _ref.disabledYearList,
      onChangeValue = _ref.onChangeValue,
      onChangeStartOfRange = _ref.onChangeStartOfRange,
      onChangeSingleValue = _ref.onChangeSingleValue,
      rest = _rollupPluginBabelHelpers.objectWithoutProperties(_ref, _excluded);
    var _useMemo = React.useMemo(function () {
        return value;
      }, [value]),
      _useMemo2 = _rollupPluginBabelHelpers.slicedToArray(_useMemo, 2),
      startExternalValue = _useMemo2[0],
      endExternalValue = _useMemo2[1];
    var _useState = React.useState([startExternalValue, endExternalValue]),
      _useState2 = _rollupPluginBabelHelpers.slicedToArray(_useState, 2),
      _useState2$ = _rollupPluginBabelHelpers.slicedToArray(_useState2[0], 2),
      startValue = _useState2$[0],
      endValue = _useState2$[1],
      setValues = _useState2[1];
    var _useState3 = React.useState(value),
      _useState4 = _rollupPluginBabelHelpers.slicedToArray(_useState3, 2),
      prevValue = _useState4[0],
      setPrevValue = _useState4[1];
    if (calendarRangeHelper.isValueUpdate(value, prevValue)) {
      setValues([startExternalValue, endExternalValue]);
      setPrevValue(value);
    }
    var handleOnChangeDay = React.useCallback(function (newDay, dateInfo) {
      if (endValue) {
        setValues([newDay, undefined]);
        onChangeStartOfRange === null || onChangeStartOfRange === void 0 || onChangeStartOfRange(newDay, dateInfo);
        return;
      }
      if (startValue) {
        setValues([startValue, newDay]);
        var _getSortedValues = calendarRangeHelper.getSortedValues([startValue, newDay]),
          _getSortedValues2 = _rollupPluginBabelHelpers.slicedToArray(_getSortedValues, 2),
          first = _getSortedValues2[0],
          second = _getSortedValues2[1];
        onChangeValue([first, second], dateInfo);
      } else {
        setValues([newDay, undefined]);
        onChangeSingleValue === null || onChangeSingleValue === void 0 || onChangeSingleValue(newDay, dateInfo);
      }
    }, [onChangeValue, onChangeStartOfRange, onChangeSingleValue, startValue, endValue]);
    React.useEffect(function () {
      setValues(value);
    }, [value[0], value[1]]);
    return /*#__PURE__*/React__default.default.createElement(Component, _rollupPluginBabelHelpers.extends({
      value: [startValue, endValue],
      onChangeValue: handleOnChangeDay,
      disabledList: disabledList,
      eventList: eventList,
      eventMonthList: eventMonthList,
      disabledMonthList: disabledMonthList,
      eventYearList: eventYearList,
      disabledYearList: disabledYearList,
      min: min,
      max: max
    }, rest));
  };
};

exports.withRange = withRange;
