import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/inherits";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
var _excluded = ["controlled", "datePicker", "labelText", "className", "id", "placeholder", "type", "onChange", "onClick", "hideLabel", "invalid", "invalidText", "helperText", "active", "value", "name", "onFocus", "onBlur", "onDragStart", "onDrop"];

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

import PropTypes from 'prop-types';
import React, { PureComponent } from 'react';
import classNames from 'classnames';
import { settings } from '../../globals/js';
var prefix = settings.prefix;
export var SingleDatePickerInput = /*#__PURE__*/function (_PureComponent) {
  _inherits(SingleDatePickerInput, _PureComponent);

  var _super = _createSuper(SingleDatePickerInput);

  function SingleDatePickerInput() {
    var _this;

    _classCallCheck(this, SingleDatePickerInput);

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

    _this = _super.call.apply(_super, [this].concat(args));

    _defineProperty(_assertThisInitialized(_this), "state", {
      controlledValue: _this.props.value ? _this.props.value : null,
      focusedInput: null
    });

    _defineProperty(_assertThisInitialized(_this), "handleFocusChange", function (focusedInput) {
      if (!focusedInput && typeof _this.props.onBlur === 'function') {
        _this.props.onBlur();
      }

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

    return _this;
  }

  _createClass(SingleDatePickerInput, [{
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props = this.props,
          controlled = _this$props.controlled,
          datePicker = _this$props.datePicker,
          labelText = _this$props.labelText,
          className = _this$props.className,
          id = _this$props.id,
          placeholder = _this$props.placeholder,
          type = _this$props.type,
          onChange = _this$props.onChange,
          onClick = _this$props.onClick,
          hideLabel = _this$props.hideLabel,
          invalid = _this$props.invalid,
          invalidText = _this$props.invalidText,
          helperText = _this$props.helperText,
          active = _this$props.active,
          value = _this$props.value,
          name = _this$props.name,
          onFocus = _this$props.onFocus,
          onBlur = _this$props.onBlur,
          onDragStart = _this$props.onDragStart,
          onDrop = _this$props.onDrop,
          other = _objectWithoutProperties(_this$props, _excluded);

      var _this$state = this.state,
          controlledValue = _this$state.controlledValue,
          focused = _this$state.focused;
      var SingleDatePicker = 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: "wfp--form-item"
      }, label, helper, /*#__PURE__*/React.createElement(SingleDatePicker, _extends({
        date: onChange && value ? value : controlledValue,
        focused: focused,
        hideKeyboardShortcutsPanel: true,
        onDateChange: function onDateChange(value) {
          if (onChange) {
            onChange({
              value: value
            });
          }

          _this2.setState({
            controlledValue: value
          }, function () {});
        },
        onFocusChange: function onFocusChange(_ref) {
          var focused = _ref.focused;

          _this2.setState({
            focused: focused
          });

          focused ? onFocus(true) : onBlur(true);
        }
      }, other)), errorMessage);
    }
  }]);

  return SingleDatePickerInput;
}(PureComponent);
SingleDatePickerInput.defaultProps = {
  onFocus: function onFocus() {},
  onBlur: function onBlur() {},
  onChange: function onChange() {}
};
SingleDatePickerInput.propTypes = {
  /**
   * Provide the SingleDatePicker as a component
   */
  datePicker: PropTypes.func.isRequired,
  onFocus: PropTypes.func,
  onBlur: PropTypes.func,

  /**
   * Provide a custom className that is applied directly to the underlying
   * node
   */
  className: PropTypes.string,

  /**
   * Specify whether the control is disabled
   */
  disabled: PropTypes.bool,

  /**
   * Provide a unique identifier for the control
   */
  id: PropTypes.string,

  /**
   * Provide the text that will be read by a screen reader when visiting this
   * control
   */
  labelText: PropTypes.node.isRequired,

  /**
   * Optionally provide an `onChange` handler that is called whenever date
   * is updated
   */
  onChange: PropTypes.func,

  /**
   * Optionally provide an `onClick` handler that is called whenever the
   * date is clicked
   */
  onClick: PropTypes.func,

  /**
   * Specify the placeholder attribute for the datepicker
   */
  placeholder: PropTypes.string,

  /**
   * Specify whether to clear selected date or not
   */
  showClearDate: PropTypes.bool,

  /**
   * Specify whether you want to show the calendar icon. It is true by default.
   */
  showDefaultInputIcon: PropTypes.bool,

  /**
   * Specify whether the control is currently invalid.
   * Either a boolean in combination with `invalidText` or an `object`( eg. { message: "Message", …otherErrorProperties }) can be passed.
   */
  invalid: PropTypes.oneOfType([PropTypes.object, PropTypes.bool]),

  /**
   * Provide the text that is displayed when the control is in an invalid state
   */
  invalidText: PropTypes.string,

  /**
   * Provide text that is used alongside the control label for additional help
   */
  helperText: PropTypes.node,

  /**
   * Specify whether you want the underlying label to be visually hidden
   */
  hideLabel: PropTypes.bool
};
export default SingleDatePickerInput;