"use strict";

var _typeof = require("@babel/runtime-corejs3/helpers/typeof");

var _WeakMap = require("@babel/runtime-corejs3/core-js-stable/weak-map");

var _Object$getOwnPropertyDescriptor = require("@babel/runtime-corejs3/core-js-stable/object/get-own-property-descriptor");

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

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

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/defineProperty"));

var _extends2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/extends"));

var _getOwnPropertySymbols = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/get-own-property-symbols"));

var _concat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/concat"));

var React = _interopRequireWildcard(require("react"));

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

var _ref2 = require("rc-util/lib/ref");

var _icons = require("mk-icon/lib/icons");

var _Input = _interopRequireDefault(require("./Input"));

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

var _SizeContext = _interopRequireDefault(require("../config-provider/SizeContext"));

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

var _reactNode = require("../_util/reactNode");

function _getRequireWildcardCache(nodeInterop) { if (typeof _WeakMap !== "function") return null; var cacheBabelInterop = new _WeakMap(); var cacheNodeInterop = new _WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && _Object$getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? _Object$getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

var __rest = void 0 && (void 0).__rest || function (s, e) {
  var t = {};

  for (var p in s) {
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
  }

  if (s != null && typeof _getOwnPropertySymbols["default"] === "function") for (var i = 0, p = (0, _getOwnPropertySymbols["default"])(s); i < p.length; i++) {
    if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
  }
  return t;
};

var Search = /*#__PURE__*/React.forwardRef(function (props, ref) {
  var inputRef = React.useRef(null);

  var onChange = function onChange(e) {
    var customOnChange = props.onChange,
        customOnSearch = props.onSearch;

    if (e && e.target && e.type === 'click' && customOnSearch) {
      customOnSearch(e.target.value, e);
    }

    if (customOnChange) {
      customOnChange(e);
    }
  };

  var onMouseDown = function onMouseDown(e) {
    var _a;

    if (document.activeElement === ((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.input)) {
      e.preventDefault();
    }
  };

  var onSearch = function onSearch(e) {
    var _a;

    var customOnSearch = props.onSearch,
        loading = props.loading,
        disabled = props.disabled;

    if (loading || disabled) {
      return;
    }

    if (customOnSearch) {
      customOnSearch((_a = inputRef.current) === null || _a === void 0 ? void 0 : _a.input.value, e);
    }
  };

  var renderLoading = function renderLoading(prefixCls) {
    var enterButton = props.enterButton,
        customizeSize = props.size;

    if (enterButton) {
      return /*#__PURE__*/React.createElement(_SizeContext["default"].Consumer, {
        key: "enterButton"
      }, function (size) {
        return /*#__PURE__*/React.createElement(_button["default"], {
          className: "".concat(prefixCls, "-button"),
          type: "primary",
          size: customizeSize || size
        }, /*#__PURE__*/React.createElement(_icons.LoadingOutlined, null));
      });
    }

    return /*#__PURE__*/React.createElement(_icons.LoadingOutlined, {
      className: "".concat(prefixCls, "-icon"),
      key: "loadingIcon"
    });
  };

  var renderSuffix = function renderSuffix(prefixCls) {
    var suffix = props.suffix,
        enterButton = props.enterButton,
        loading = props.loading;

    if (loading && !enterButton) {
      return [suffix, renderLoading(prefixCls)];
    }

    if (enterButton) return suffix;
    var icon = /*#__PURE__*/React.createElement(_icons.SearchOutlined, {
      className: "".concat(prefixCls, "-icon"),
      key: "searchIcon",
      onClick: onSearch
    });

    if (suffix) {
      return [(0, _reactNode.replaceElement)(suffix, null, {
        key: 'suffix'
      }), icon];
    }

    return icon;
  };

  var renderAddonAfter = function renderAddonAfter(prefixCls, size) {
    var enterButton = props.enterButton,
        disabled = props.disabled,
        addonAfter = props.addonAfter,
        loading = props.loading;
    var btnClassName = "".concat(prefixCls, "-button");

    if (loading && enterButton) {
      return [renderLoading(prefixCls), addonAfter];
    }

    if (!enterButton) return addonAfter;
    var button;
    var enterButtonAsElement = enterButton;
    var isAntdButton = enterButtonAsElement.type && enterButtonAsElement.type.__ANT_BUTTON === true;

    if (isAntdButton || enterButtonAsElement.type === 'button') {
      button = (0, _reactNode.cloneElement)(enterButtonAsElement, (0, _extends2["default"])({
        onMouseDown: onMouseDown,
        onClick: onSearch,
        key: 'enterButton'
      }, isAntdButton ? {
        className: btnClassName,
        size: size
      } : {}));
    } else {
      button = /*#__PURE__*/React.createElement(_button["default"], {
        className: btnClassName,
        type: "primary",
        size: size,
        disabled: disabled,
        key: "enterButton",
        onMouseDown: onMouseDown,
        onClick: onSearch
      }, enterButton === true ? /*#__PURE__*/React.createElement(_icons.SearchOutlined, null) : enterButton);
    }

    if (addonAfter) {
      return [button, (0, _reactNode.replaceElement)(addonAfter, null, {
        key: 'addonAfter'
      })];
    }

    return button;
  };

  var renderSearch = function renderSearch(_ref) {
    var getPrefixCls = _ref.getPrefixCls,
        direction = _ref.direction;

    var customizePrefixCls = props.prefixCls,
        customizeInputPrefixCls = props.inputPrefixCls,
        enterButton = props.enterButton,
        className = props.className,
        customizeSize = props.size,
        restProps = __rest(props, ["prefixCls", "inputPrefixCls", "enterButton", "className", "size"]);

    delete restProps.onSearch;
    delete restProps.loading;
    var prefixCls = getPrefixCls('input-search', customizePrefixCls);
    var inputPrefixCls = getPrefixCls('input', customizeInputPrefixCls);

    var getClassName = function getClassName(size) {
      var inputClassName;

      if (enterButton) {
        var _context, _classNames;

        inputClassName = (0, _classnames["default"])(prefixCls, className, (_classNames = {}, (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-rtl"), direction === 'rtl'), (0, _defineProperty2["default"])(_classNames, "".concat(prefixCls, "-enter-button"), !!enterButton), (0, _defineProperty2["default"])(_classNames, (0, _concat["default"])(_context = "".concat(prefixCls, "-")).call(_context, size), !!size), _classNames));
      } else {
        inputClassName = (0, _classnames["default"])(prefixCls, className, (0, _defineProperty2["default"])({}, "".concat(prefixCls, "-rtl"), direction === 'rtl'));
      }

      return inputClassName;
    };

    return /*#__PURE__*/React.createElement(_SizeContext["default"].Consumer, null, function (size) {
      return /*#__PURE__*/React.createElement(_Input["default"], (0, _extends2["default"])({
        ref: (0, _ref2.composeRef)(inputRef, ref),
        onPressEnter: onSearch
      }, restProps, {
        size: customizeSize || size,
        prefixCls: inputPrefixCls,
        addonAfter: renderAddonAfter(prefixCls, customizeSize || size),
        suffix: renderSuffix(prefixCls),
        onChange: onChange,
        className: getClassName(customizeSize || size)
      }));
    });
  };

  return /*#__PURE__*/React.createElement(_configProvider.ConfigConsumer, null, renderSearch);
});
Search.defaultProps = {
  enterButton: false
};
Search.displayName = 'Search';
var _default = Search;
exports["default"] = _default;