import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
var _excluded = ["startDate", "endDate", "controlled", "datePicker", "labelText", "className", "id", "placeholder", "type", "onChange", "onClick", "hideLabel", "invalid", "invalidText", "helperText", "value", "startDateId", "startDatePlaceholderText", "endDateId", "endDatePlaceholderText", "name", "onBlur", "onDragStart", "onDrop", "onFocus"];
import PropTypes from 'prop-types';
import React, { memo, useState } from 'react';
import { DateRangePickerInput } from './DateRangePickerInput';
import classNames from 'classnames';
import { settings } from '../../globals/js';
var prefix = settings.prefix;
export default /*#__PURE__*/memo(function DateRangePickerInput(_ref) {
  var startDate = _ref.startDate,
      endDate = _ref.endDate,
      controlled = _ref.controlled,
      datePicker = _ref.datePicker,
      labelText = _ref.labelText,
      className = _ref.className,
      id = _ref.id,
      placeholder = _ref.placeholder,
      type = _ref.type,
      onChange = _ref.onChange,
      onClick = _ref.onClick,
      hideLabel = _ref.hideLabel,
      invalid = _ref.invalid,
      invalidText = _ref.invalidText,
      helperText = _ref.helperText,
      value = _ref.value,
      startDateId = _ref.startDateId,
      startDatePlaceholderText = _ref.startDatePlaceholderText,
      endDateId = _ref.endDateId,
      endDatePlaceholderText = _ref.endDatePlaceholderText,
      name = _ref.name,
      onBlur = _ref.onBlur,
      onDragStart = _ref.onDragStart,
      onDrop = _ref.onDrop,
      onFocus = _ref.onFocus,
      other = _objectWithoutProperties(_ref, _excluded);

  var _useState = useState(null),
      _useState2 = _slicedToArray(_useState, 2),
      focusedInput = _useState2[0],
      setfocusedInput = _useState2[1];

  var _useState3 = useState(startDate ? startDate : null),
      _useState4 = _slicedToArray(_useState3, 2),
      stateStartDate = _useState4[0],
      setstartDate = _useState4[1];

  var _useState5 = useState(endDate ? endDate : null),
      _useState6 = _slicedToArray(_useState5, 2),
      stateEndDate = _useState6[0],
      setendDate = _useState6[1];

  var handleFocusChange = function handleFocusChange(focusedInput) {
    if (!focusedInput && typeof onBlur === 'function') {
      onBlur();
    }

    setfocusedInput(focusedInput);
  }; // const { focusedInput, startDate, endDate } = this.state;


  var DateRangePicker = datePicker;
  var labelClasses = classNames("".concat(prefix, "--label"), _defineProperty({}, "".concat(prefix, "--visually-hidden"), hideLabel));
  var label = labelText ? /*#__PURE__*/React.createElement("label", {
    htmlFor: id,
    className: labelClasses
  }, labelText) : null;
  var errorMessage = invalid ? /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefix, "--form-requirement invalid")
  }, invalidText) : null;
  var helper = helperText ? /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefix, "--form__helper-text")
  }, helperText) : null;
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefix, "--form-item")
  }, label, helper, /*#__PURE__*/React.createElement(DateRangePicker, _extends({
    endDateId: endDateId,
    endDate: onChange && value ? value.endDate : stateEndDate,
    endDatePlaceholderText: endDatePlaceholderText,
    focusedInput: focusedInput,
    hideKeyboardShortcutsPanel: true,
    onDatesChange: function onDatesChange(_ref2) {
      var startDate = _ref2.startDate,
          endDate = _ref2.endDate;

      if (onChange) {
        onChange({
          startDate: startDate,
          endDate: endDate
        });
      }

      setstartDate(startDate);
      setendDate(endDate);
    },
    onFocusChange: handleFocusChange,
    startDateId: startDateId,
    startDate: onChange && value ? value.startDate : stateStartDate,
    startDatePlaceholderText: startDatePlaceholderText
  }, other)), errorMessage);
});
DateRangePickerInput.defaultProps = {
  startDateId: 'startDate',
  endDateId: 'endDate',
  startDatePlaceholderText: 'Start Date',
  endDatePlaceholderText: 'End Date'
};
DateRangePickerInput.propTypes = {
  /**
   * Provide the SingleDatePicker as a component
   */
  datePicker: PropTypes.func.isRequired,

  /**
   * Provide the placeholder for the start input
   */
  startDatePlaceholderText: PropTypes.string,

  /**
   * Provide the placeholder for the end input
   */
  endDatePlaceholderText: PropTypes.string
}; // export default DateRangePickerInput;