import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _extends from "@babel/runtime/helpers/extends";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
var _excluded = ["className", "inputPrefixCls", "size", "suffix", "enterButton"];
import React, { cloneElement, Component } from 'react';
import classNames from 'classnames';
import Input from './Input';
import Button from '../button';
import { Size } from '../_util/enum';
import ConfigContext from '../config-provider/ConfigContext';
import Icon from '../icon';

var Search = /*#__PURE__*/function (_Component) {
  _inherits(Search, _Component);

  var _super = _createSuper(Search);

  function Search() {
    var _this;

    _classCallCheck(this, Search);

    _this = _super.apply(this, arguments);

    _this.onSearch = function (e) {
      var onSearch = _this.props.onSearch;

      if (onSearch) {
        onSearch(_this.input.input.value, e);
      }

      _this.input.focus();
    };

    _this.saveInput = function (node) {
      _this.input = node;
    };

    return _this;
  }

  _createClass(Search, [{
    key: "focus",
    value: function focus() {
      this.input.focus();
    }
  }, {
    key: "blur",
    value: function blur() {
      this.input.blur();
    }
  }, {
    key: "getPrefixCls",
    value: function getPrefixCls() {
      var prefixCls = this.props.prefixCls;
      var getPrefixCls = this.context.getPrefixCls;
      return getPrefixCls('input-search', prefixCls);
    }
  }, {
    key: "getButtonOrIcon",
    value: function getButtonOrIcon(prefixCls) {
      var _this2 = this;

      var _this$props = this.props,
          enterButton = _this$props.enterButton,
          size = _this$props.size,
          disabled = _this$props.disabled,
          buttonProps = _this$props.buttonProps;

      if (!enterButton) {
        return /*#__PURE__*/React.createElement(Icon, {
          className: "".concat(prefixCls, "-icon"),
          type: "search",
          key: "searchIcon",
          onClick: this.onSearch
        });
      }

      var enterButtonAsElement = enterButton;
      var isButton = enterButtonAsElement.type && (enterButtonAsElement.type.__C7N_BUTTON === true || enterButtonAsElement.type.__PRO_BUTTON === true || enterButtonAsElement.type.__ANT_BUTTON === true);

      if (isButton || enterButtonAsElement.type === 'button') {
        return /*#__PURE__*/cloneElement(enterButtonAsElement, isButton ? {
          className: "".concat(prefixCls, "-button"),
          size: size,
          onClick: function onClick(e) {
            if (enterButtonAsElement) {
              var props = enterButtonAsElement.props;

              if (props && props.onClick) {
                props.onClick(e);
              }
            }

            _this2.onSearch(e);
          }
        } : {
          onClick: this.onSearch
        });
      }

      return /*#__PURE__*/React.createElement(Button, _extends({}, buttonProps, {
        className: "".concat(prefixCls, "-button"),
        type: "primary",
        size: size,
        disabled: disabled,
        key: "enterButton",
        icon: enterButton === true || !enterButton ? 'search' : undefined,
        onClick: this.onSearch
      }), enterButton === true ? undefined : enterButton);
    }
  }, {
    key: "render",
    value: function render() {
      var _classNames;

      var _this$props2 = this.props,
          className = _this$props2.className,
          inputPrefixCls = _this$props2.inputPrefixCls,
          size = _this$props2.size,
          suffix = _this$props2.suffix,
          enterButton = _this$props2.enterButton,
          others = _objectWithoutProperties(_this$props2, _excluded);

      var getPrefixCls = this.context.getPrefixCls;
      var prefixCls = this.getPrefixCls();
      delete others.onSearch;
      delete others.prefixCls;
      var buttonOrIcon = this.getButtonOrIcon(prefixCls);
      var searchSuffix = suffix ? [suffix, buttonOrIcon] : buttonOrIcon;
      var inputClassName = classNames(prefixCls, className, (_classNames = {}, _defineProperty(_classNames, "".concat(prefixCls, "-enter-button"), !!enterButton), _defineProperty(_classNames, "".concat(prefixCls, "-").concat(size), !!size), _classNames));
      return /*#__PURE__*/React.createElement(Input, _extends({
        onPressEnter: this.onSearch
      }, others, {
        size: size,
        className: inputClassName,
        prefixCls: getPrefixCls('input', inputPrefixCls),
        suffix: searchSuffix,
        ref: this.saveInput
      }));
    }
  }], [{
    key: "contextType",
    get: function get() {
      return ConfigContext;
    }
  }]);

  return Search;
}(Component);

export { Search as default };
Search.displayName = 'Search';
Search.defaultProps = {
  enterButton: true,
  size: Size.small
};
//# sourceMappingURL=Search.js.map
