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 = ["className", "connectedLeft", "connectedRight", "disabled", "error", "forwardedRef", "helpText", "onFocus", "onBlur", "prefix", "inverse", "readOnly", "success", "suffix", "width", "warning", "noInputStyling"];

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

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 React, { PureComponent, forwardRef } from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
import InputBase from './InputBase';
import Box, { omitBoxProps, pickBoxProps } from '../box';
import ValidationText from '../validationText';
import theme from './theme.css';

var SingleLineInputBase = /*#__PURE__*/function (_PureComponent) {
  _inherits(SingleLineInputBase, _PureComponent);

  var _super = _createSuper(SingleLineInputBase);

  function SingleLineInputBase() {
    var _this;

    _classCallCheck(this, SingleLineInputBase);

    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", {
      inputHasfocus: false
    });

    _defineProperty(_assertThisInitialized(_this), "handleBlur", function (event) {
      _this.setState({
        inputHasfocus: false
      });

      if (_this.props.onBlur) {
        _this.props.onBlur(event);
      }
    });

    _defineProperty(_assertThisInitialized(_this), "handleFocus", function (event) {
      _this.setState({
        inputHasfocus: true
      });

      if (_this.props.onFocus) {
        _this.props.onFocus(event);
      }
    });

    return _this;
  }

  _createClass(SingleLineInputBase, [{
    key: "renderOneOrMultipleElements",
    value: function renderOneOrMultipleElements(prop) {
      if (Array.isArray(prop)) {
        return prop.map(function (element, index) {
          return /*#__PURE__*/React.cloneElement(element, {
            key: index
          });
        });
      }

      return prop;
    }
  }, {
    key: "render",
    value: function render() {
      var _cx;

      var _this$props = this.props,
          className = _this$props.className,
          connectedLeft = _this$props.connectedLeft,
          connectedRight = _this$props.connectedRight,
          disabled = _this$props.disabled,
          error = _this$props.error,
          forwardedRef = _this$props.forwardedRef,
          helpText = _this$props.helpText,
          onFocus = _this$props.onFocus,
          onBlur = _this$props.onBlur,
          prefix = _this$props.prefix,
          inverse = _this$props.inverse,
          readOnly = _this$props.readOnly,
          success = _this$props.success,
          suffix = _this$props.suffix,
          width = _this$props.width,
          warning = _this$props.warning,
          noInputStyling = _this$props.noInputStyling,
          others = _objectWithoutProperties(_this$props, _excluded);

      var classNames = cx(theme['wrapper'], (_cx = {}, _defineProperty(_cx, theme['has-focus'], this.state.inputHasfocus), _defineProperty(_cx, theme['has-error'], error), _defineProperty(_cx, theme['has-success'], success), _defineProperty(_cx, theme['has-warning'], warning), _defineProperty(_cx, theme['has-connected-left'], connectedLeft), _defineProperty(_cx, theme['has-connected-right'], connectedRight), _defineProperty(_cx, theme['is-disabled'], disabled), _defineProperty(_cx, theme['is-inverse'], inverse), _defineProperty(_cx, theme['is-read-only'], readOnly), _cx), className);
      var boxProps = pickBoxProps(others);

      var inputProps = _objectSpread({
        ref: forwardedRef,
        disabled: disabled,
        inverse: inverse,
        onBlur: this.handleBlur,
        onFocus: this.handleFocus,
        readOnly: readOnly
      }, omitBoxProps(others));

      return /*#__PURE__*/React.createElement(Box, _extends({
        className: classNames
      }, boxProps), /*#__PURE__*/React.createElement("div", {
        className: cx(theme['input-wrapper'], noInputStyling && theme['is-input-disabled'])
      }, connectedLeft, /*#__PURE__*/React.createElement("div", {
        className: theme['input-inner-wrapper'],
        style: {
          width: width,
          flex: width && '0 1 auto'
        }
      }, prefix && /*#__PURE__*/React.createElement("div", {
        className: theme['prefix-wrapper']
      }, this.renderOneOrMultipleElements(prefix)), /*#__PURE__*/React.createElement(InputBase, inputProps), suffix && /*#__PURE__*/React.createElement("div", {
        className: theme['suffix-wrapper']
      }, this.renderOneOrMultipleElements(suffix))), connectedRight), /*#__PURE__*/React.createElement(ValidationText, {
        error: error,
        help: helpText,
        inverse: inverse,
        success: success,
        warning: warning
      }));
    }
  }]);

  return SingleLineInputBase;
}(PureComponent);

/** @type {any} */
var ForwardedSingleInputBase = /*#__PURE__*/forwardRef(function (props, ref) {
  return /*#__PURE__*/React.createElement(SingleLineInputBase, _extends({}, props, {
    forwardedRef: ref
  }));
});
ForwardedSingleInputBase.displayName = 'SingleLineInputBase';
export default ForwardedSingleInputBase;