"use strict";

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

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

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

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

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

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

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

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

var _TextInputControl = require("../TextInputControl");

var _lodash = _interopRequireDefault(require("lodash"));

var _reactDates = require("react-dates");

var _datePickerHelpers = require("./datePickerHelpers");

var _moment = _interopRequireDefault(require("moment"));

var _PreDefinedRangesBlock = require("./PreDefinedRangesBlock");

var _DatePickerBase2 = require("./DatePickerBase");

var _renderCalendarDayContent = require("./renderCalendarDayContent");

var DayRangePickerControl =
/*#__PURE__*/
function (_DatePickerBase) {
  (0, _inheritsLoose2.default)(DayRangePickerControl, _DatePickerBase);

  function DayRangePickerControl() {
    var _this;

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _DatePickerBase.call.apply(_DatePickerBase, [this].concat(args)) || this;
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "state", Object.assign({}, _DatePickerBase.prototype.state, {
      startDate: _this.props.startDate,
      endDate: _this.props.endDate,
      focusedInput: _datePickerHelpers.REACT_DATES_START_DATE_INPUT_MODE,
      value: _this.getPropsFromContext().value
    }));
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onCalendarFocusChange", function (focusedInput) {
      return _this.setState({
        focusedInput: focusedInput
      });
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "notifyDateTimeChange", function (_ref) {
      var startDate = _ref.startDate,
          endDateValue = _ref.endDate;

      var _this$getPropsFromCon = _this.getPropsFromContext(),
          onChangeFromContext = _this$getPropsFromCon.onChange;

      var onChangeFromProp = _this.props.onChange;
      var endDate = endDateValue === null ? startDate : endDateValue;

      if (_lodash.default.isFunction(onChangeFromProp)) {
        onChangeFromProp({
          startDate: startDate,
          endDate: endDate
        });
      } else if (_lodash.default.isFunction(onChangeFromContext)) {
        onChangeFromContext({
          startDate: startDate,
          endDate: endDate
        });
      }
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onDatesChange", function (_ref2) {
      var startDate = _ref2.startDate,
          endDate = _ref2.endDate;

      _this.setState({
        startDate: startDate,
        endDate: endDate
      }, function () {
        return _this.notifyDateTimeChange({
          startDate: startDate,
          endDate: endDate
        });
      });
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "onPredefinedRangeButtonClick", function (_ref3) {
      var startDate = _ref3.startDate,
          endDate = _ref3.endDate;

      _this.onDatesChange({
        startDate: startDate,
        endDate: endDate
      });

      _this.closeDayPicker();

      _this.toggleIsInputFocusedFromOutside(false);

      _this.focusToInput();
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "renderPredefinedRanges", function () {
      var ranges = _this.props.ranges;
      var _this$state = _this.state,
          startDate = _this$state.startDate,
          endDate = _this$state.endDate;
      return _react.default.createElement(_PreDefinedRangesBlock.PreDefinedRangesBlock, {
        ranges: ranges,
        startDate: startDate,
        endDate: endDate,
        onPredefinedRangeButtonClick: _this.onPredefinedRangeButtonClick
      });
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "isOutsideRange", function (dayValue) {
      var day = (0, _moment.default)(dayValue).startOf('date');
      var _this$props = _this.props,
          maxDate = _this$props.maxDate,
          minDate = _this$props.minDate;
      var maxDateMoment = maxDate ? (0, _moment.default)(maxDate) : maxDate;
      var minDateMoment = minDate ? (0, _moment.default)(minDate) : minDate;

      if (maxDateMoment && minDateMoment) {
        return (0, _moment.default)(minDateMoment).isAfter(day) || (0, _moment.default)(maxDateMoment).isSameOrBefore(day);
      }

      if (maxDateMoment) {
        return (0, _moment.default)(maxDateMoment).isBefore(day);
      }

      if (minDateMoment) {
        return (0, _moment.default)(minDateMoment).isAfter(day);
      }

      return false;
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "handleCloseDayPicker", function (dateRange) {
      var focusedInput = _this.state.focusedInput;

      _this.toggleIsInputFocusedFromOutside(false);

      _this.closeDayPicker();

      _this.setState({
        focusedInput: _datePickerHelpers.REACT_DATES_START_DATE_INPUT_MODE
      });

      if (!_lodash.default.isEmpty(dateRange)) {
        _this.onDatesChange(dateRange);
      }

      if (focusedInput !== _datePickerHelpers.REACT_DATES_START_DATE_INPUT_MODE) {
        _this.focusToInput();
      }
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "getDisplayValue", function () {
      var _this$state2 = _this.state,
          startDate = _this$state2.startDate,
          endDate = _this$state2.endDate;
      var valueFormatter = _this.props.valueFormatter;

      if (_lodash.default.isEmpty(startDate) || _lodash.default.isEmpty(endDate)) {
        return '';
      }

      return valueFormatter(startDate, endDate);
    });
    (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "renderDatePicker", function () {
      var _this$props2 = _this.props,
          numberOfMonths = _this$props2.numberOfMonths,
          dayPickerProps = _this$props2.dayPickerProps;
      var _this$state3 = _this.state,
          startDate = _this$state3.startDate,
          endDate = _this$state3.endDate,
          focusedInput = _this$state3.focusedInput,
          isDayPickerFocused = _this$state3.isDayPickerFocused,
          isDayPickerVisible = _this$state3.isDayPickerVisible;

      if (!isDayPickerVisible) {
        return null;
      }

      return _react.default.createElement(_reactDates.DayPickerRangeController, (0, _extends2.default)({
        isFocused: isDayPickerFocused,
        focusedInput: focusedInput,
        numberOfMonths: numberOfMonths,
        onFocusChange: _this.onCalendarFocusChange,
        onDatesChange: _this.onDatesChange,
        onClose: _this.handleCloseDayPicker,
        startDate: startDate,
        endDate: endDate,
        renderCalendarInfo: _this.renderPredefinedRanges,
        isOutsideRange: _this.isOutsideRange,
        calendarInfoPosition: "before",
        hideKeyboardShortcutsPanel: true,
        phrases: _this.getPhrases(),
        renderDayContents: _renderCalendarDayContent.renderCalendarDayContent,
        minimumNights: 0
      }, dayPickerProps));
    });
    return _this;
  }

  var _proto = DayRangePickerControl.prototype;

  _proto.componentWillReceiveProps = function componentWillReceiveProps(newProps) {
    var newStartDate = newProps.startDate,
        newEndDate = newProps.endDate;
    var _this$props3 = this.props,
        startDate = _this$props3.startDate,
        endDate = _this$props3.endDate;
    var newStates = {};

    if (!(0, _datePickerHelpers.isSameDate)(startDate, newStartDate)) {
      newStates.startDate = newStartDate;
    }

    if (!(0, _datePickerHelpers.isSameDate)(endDate, newEndDate)) {
      newStates.endDate = newEndDate;
    }

    if (!_lodash.default.isEmpty(newStates)) {
      this.setState(newStates);
    }
  };

  /**@override*/
  _proto.closeDayPicker = function closeDayPicker() {
    this.setState({
      focusedInput: _datePickerHelpers.REACT_DATES_START_DATE_INPUT_MODE
    });
    return _DatePickerBase.prototype.closeDayPicker.call(this);
  }
  /** @override */
  ;

  return DayRangePickerControl;
}(_DatePickerBase2.DatePickerBase);

exports.DayRangePickerControl = DayRangePickerControl;
(0, _defineProperty2.default)(DayRangePickerControl, "propTypes", Object.assign({}, _TextInputControl.TextInputControl.propTypes, _DatePickerBase2.DatePickerBase.propTypes, {
  startDate: _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.object]),
  endDate: _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.object]),
  valueFormatter: _propTypes.default.func,
  onChange: _propTypes.default.func,
  numberOfMonths: _propTypes.default.number,
  dayPickerProps: _propTypes.default.object,
  inputProps: _propTypes.default.object,
  ranges: _propTypes.default.object,
  maxDate: _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.object]),
  minDate: _propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.object])
}));
(0, _defineProperty2.default)(DayRangePickerControl, "defaultProps", Object.assign({}, _TextInputControl.TextInputControl.defaultProps, _DatePickerBase2.DatePickerBase.defaultProps, {
  testAutomationId: 'dayRangePicker',
  startDate: null,
  endDate: null,
  valueFormatter: _datePickerHelpers.dateRangePickerValueFormatter,
  dayPickerProps: {},
  numberOfMonths: 2,
  inputProps: {},
  ranges: null,
  maxDate: null,
  minDate: null,
  ariaLabel: 'TRANSLATE:CHOOSE_DATE_RANGE'
}));