import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import React from 'react';
import cx from 'classnames';
import Button from '../button';
import Input from './input';
import { cloneElement } from '../utils/ReactNodeValidate';
import { ConfigContext } from '../config-provider/context';
import Icon from '../icon';
var SearchLine = Icon.SearchLine;
var SearchInput = /*#__PURE__*/React.forwardRef(function (props, ref) {
  var loading = props.loading,
      enterButton = props.enterButton,
      onSearch = props.onSearch,
      inputProps = _objectWithoutProperties(props, ["loading", "enterButton", "onSearch"]); // console.log(enterButton, '--------enterButton');


  var _React$useContext = React.useContext(ConfigContext),
      getPrefixClassName = _React$useContext.getPrefixClassName;

  var inputCls = getPrefixClassName('input-search');
  var inputRef = ref || React.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.createElement(Button, {
        type: "primary",
        size: props.size,
        loading: loading,
        disabled: props.disabled || loading,
        onClick: handleSearch,
        className: cx("".concat(inputCls, "-btn"), loading && "".concat(inputCls, "-btn-loading"))
      }, loading ? '加载中' : enterButton);
    }

    return enterButton;
  };

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

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

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

  return /*#__PURE__*/React.createElement(Input, Object.assign({}, inputProps, {
    ref: inputRef,
    onPressEnter: handleSearch,
    className: cx(inputCls, props.className)
  }));
});
SearchInput.displayName = 'SearchInput';
export default SearchInput;