import _extends from "@babel/runtime/helpers/extends";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import React from 'react';
import PropTypes from 'prop-types';
import { TextInputControl } from '../TextInputControl';
import _ from 'lodash';
import { DayPickerRangeController } from 'react-dates';
import { dateRangePickerValueFormatter, isSameDate, REACT_DATES_START_DATE_INPUT_MODE } from './datePickerHelpers';
import moment from 'moment';
import { PreDefinedRangesBlock } from './PreDefinedRangesBlock';
import { DatePickerBase } from './DatePickerBase';
import { renderCalendarDayContent } from './renderCalendarDayContent';
export var DayRangePickerControl =
/*#__PURE__*/
function (_DatePickerBase) {
  _inheritsLoose(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;

    _defineProperty(_assertThisInitialized(_this), "state", Object.assign({}, _DatePickerBase.prototype.state, {
      startDate: _this.props.startDate,
      endDate: _this.props.endDate,
      focusedInput: REACT_DATES_START_DATE_INPUT_MODE,
      value: _this.getPropsFromContext().value
    }));

    _defineProperty(_assertThisInitialized(_this), "onCalendarFocusChange", function (focusedInput) {
      return _this.setState({
        focusedInput: focusedInput
      });
    });

    _defineProperty(_assertThisInitialized(_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 (_.isFunction(onChangeFromProp)) {
        onChangeFromProp({
          startDate: startDate,
          endDate: endDate
        });
      } else if (_.isFunction(onChangeFromContext)) {
        onChangeFromContext({
          startDate: startDate,
          endDate: endDate
        });
      }
    });

    _defineProperty(_assertThisInitialized(_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
        });
      });
    });

    _defineProperty(_assertThisInitialized(_this), "onPredefinedRangeButtonClick", function (_ref3) {
      var startDate = _ref3.startDate,
          endDate = _ref3.endDate;

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

      _this.closeDayPicker();

      _this.toggleIsInputFocusedFromOutside(false);

      _this.focusToInput();
    });

    _defineProperty(_assertThisInitialized(_this), "renderPredefinedRanges", function () {
      var ranges = _this.props.ranges;
      var _this$state = _this.state,
          startDate = _this$state.startDate,
          endDate = _this$state.endDate;
      return React.createElement(PreDefinedRangesBlock, {
        ranges: ranges,
        startDate: startDate,
        endDate: endDate,
        onPredefinedRangeButtonClick: _this.onPredefinedRangeButtonClick
      });
    });

    _defineProperty(_assertThisInitialized(_this), "isOutsideRange", function (dayValue) {
      var day = moment(dayValue).startOf('date');
      var _this$props = _this.props,
          maxDate = _this$props.maxDate,
          minDate = _this$props.minDate;
      var maxDateMoment = maxDate ? moment(maxDate) : maxDate;
      var minDateMoment = minDate ? moment(minDate) : minDate;

      if (maxDateMoment && minDateMoment) {
        return moment(minDateMoment).isAfter(day) || moment(maxDateMoment).isSameOrBefore(day);
      }

      if (maxDateMoment) {
        return moment(maxDateMoment).isBefore(day);
      }

      if (minDateMoment) {
        return moment(minDateMoment).isAfter(day);
      }

      return false;
    });

    _defineProperty(_assertThisInitialized(_this), "handleCloseDayPicker", function (dateRange) {
      var focusedInput = _this.state.focusedInput;

      _this.toggleIsInputFocusedFromOutside(false);

      _this.closeDayPicker();

      _this.setState({
        focusedInput: REACT_DATES_START_DATE_INPUT_MODE
      });

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

      if (focusedInput !== REACT_DATES_START_DATE_INPUT_MODE) {
        _this.focusToInput();
      }
    });

    _defineProperty(_assertThisInitialized(_this), "getDisplayValue", function () {
      var _this$state2 = _this.state,
          startDate = _this$state2.startDate,
          endDate = _this$state2.endDate;
      var valueFormatter = _this.props.valueFormatter;

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

      return valueFormatter(startDate, endDate);
    });

    _defineProperty(_assertThisInitialized(_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.createElement(DayPickerRangeController, _extends({
        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,
        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 (!isSameDate(startDate, newStartDate)) {
      newStates.startDate = newStartDate;
    }

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

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

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

  return DayRangePickerControl;
}(DatePickerBase);

_defineProperty(DayRangePickerControl, "propTypes", Object.assign({}, TextInputControl.propTypes, DatePickerBase.propTypes, {
  startDate: PropTypes.oneOfType([PropTypes.number, PropTypes.object]),
  endDate: PropTypes.oneOfType([PropTypes.number, PropTypes.object]),
  valueFormatter: PropTypes.func,
  onChange: PropTypes.func,
  numberOfMonths: PropTypes.number,
  dayPickerProps: PropTypes.object,
  inputProps: PropTypes.object,
  ranges: PropTypes.object,
  maxDate: PropTypes.oneOfType([PropTypes.number, PropTypes.object]),
  minDate: PropTypes.oneOfType([PropTypes.number, PropTypes.object])
}));

_defineProperty(DayRangePickerControl, "defaultProps", Object.assign({}, TextInputControl.defaultProps, DatePickerBase.defaultProps, {
  testAutomationId: 'dayRangePicker',
  startDate: null,
  endDate: null,
  valueFormatter: dateRangePickerValueFormatter,
  dayPickerProps: {},
  numberOfMonths: 2,
  inputProps: {},
  ranges: null,
  maxDate: null,
  minDate: null,
  ariaLabel: 'TRANSLATE:CHOOSE_DATE_RANGE'
}));