"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));

var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));

var _react = _interopRequireDefault(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _button = _interopRequireDefault(require("../button"));

var _input = _interopRequireDefault(require("./input"));

var _ReactNodeValidate = require("../utils/ReactNodeValidate");

var _context = require("../config-provider/context");

var _icon = _interopRequireDefault(require("../icon"));

var SearchLine = _icon.default.SearchLine;

var SearchInput = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
  var loading = props.loading,
      enterButton = props.enterButton,
      onSearch = props.onSearch,
      inputProps = (0, _objectWithoutProperties2.default)(props, ["loading", "enterButton", "onSearch"]); // console.log(enterButton, '--------enterButton');

  var _React$useContext = _react.default.useContext(_context.ConfigContext),
      getPrefixClassName = _React$useContext.getPrefixClassName;

  var inputCls = getPrefixClassName('input-search');

  var inputRef = ref || _react.default.useRef(null); // handle


  var handleSearch = function handleSearch(evt) {
    var value = inputRef.current ? inputRef.current.value : '';
    onSearch === null || onSearch === void 0 ? void 0 : onSearch(value, evt);
  }; // render


  var renderSearchBtn = function renderSearchBtn() {
    if (typeof enterButton === 'string') {
      return /*#__PURE__*/_react.default.createElement(_button.default, {
        type: "primary",
        size: props.size,
        loading: loading,
        disabled: props.disabled || loading,
        onClick: handleSearch,
        className: (0, _classnames.default)("".concat(inputCls, "-btn"), loading && "".concat(inputCls, "-btn-loading"))
      }, loading ? '加载中' : enterButton);
    }

    return enterButton;
  };

  if (typeof enterButton === 'undefined') {
    inputProps.suffix = /*#__PURE__*/_react.default.createElement("span", {
      onClick: handleSearch,
      className: "".concat(inputCls, "-default-search")
    }, /*#__PURE__*/_react.default.createElement(SearchLine, null));
  } else {
    inputProps.addonAfter = renderSearchBtn();
  }

  var enterButtonAsElement = enterButton || {};
  var isAntdButton = enterButtonAsElement.type;

  if (isAntdButton) {
    inputProps.addonAfter = (0, _ReactNodeValidate.cloneElement)(enterButtonAsElement, (0, _objectSpread2.default)({
      onMouseDown: handleSearch,
      // onClick: handleSearch,
      key: 'enterButton'
    }, isAntdButton ? {
      className: "".concat(inputCls, "-btn"),
      size: props.size
    } : {}));
  }

  return /*#__PURE__*/_react.default.createElement(_input.default, Object.assign({}, inputProps, {
    ref: inputRef,
    onPressEnter: handleSearch,
    className: (0, _classnames.default)(inputCls, props.className)
  }));
});

SearchInput.displayName = 'SearchInput';
var _default = SearchInput;
exports.default = _default;