function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }

function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

import React, { useState, useCallback, useRef, useEffect } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import ConfigProvider from '../../config-provider';
import Search from '../Search';
import Animate from '../../animate';
import Balloon from '../../balloon';
import Button from '../../button';
import { isFunction } from '../../util/func';
var Tooltip = Balloon.Tooltip;
var IconSearchFC = React.memo(function (_ref) {
  var prefix = _ref.prefix,
      className = _ref.className,
      disabled = _ref.disabled,
      onFocus = _ref.onFocus,
      onChange = _ref.onChange,
      propsValue = _ref.value,
      onBlur = _ref.onBlur,
      direction = _ref.direction,
      propsSearchCls = _ref.searchCls,
      open = _ref.open,
      onOpen = _ref.onOpen,
      triggerTooltipProps = _ref.triggerTooltipProps,
      searchProps = _objectWithoutProperties(_ref, ["prefix", "className", "disabled", "onFocus", "onChange", "value", "onBlur", "direction", "searchCls", "open", "onOpen", "triggerTooltipProps"]);

  var _useState = useState(propsValue),
      _useState2 = _slicedToArray(_useState, 2),
      value = _useState2[0],
      setValue = _useState2[1];

  var searchRef = useRef(null);
  var rootCls = "".concat(prefix, "iconsearch");
  var wrapperCls = classNames(rootCls, className, {
    rtl: direction === 'rtl'
  });
  var searchCls = classNames("".concat(rootCls, "-search"), propsSearchCls, _defineProperty({}, "".concat(rootCls, "-fold"), !open));
  var handleFocus = useCallback(function (e) {
    onOpen(true);
    onFocus && onFocus(e);
  }, [onFocus, onOpen]);
  var handleBlur = useCallback(function (e) {
    if (value === '' || value === undefined) {
      onOpen(false);
    }

    onBlur && onBlur(e);
  }, [onBlur, value, onOpen]);
  var handleClick = useCallback(function () {
    if (!open) {
      onOpen(true);
      return;
    }

    setTimeout(function () {
      if (searchRef.current && searchRef.current.getInstance) {
        var instance = searchRef.current.getInstance();
        instance && instance.focus && instance.focus();
      }
    }, 50);
  });
  var handleChange = useCallback(function (v) {
    setValue(v);

    for (var _len = arguments.length, argv = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
      argv[_key - 1] = arguments[_key];
    }

    onChange && onChange.apply(void 0, [v].concat(argv));
  }, [onChange]);
  useEffect(function () {
    return setValue(propsValue);
  }, [propsValue]);
  useEffect(function () {
    open && handleClick();
  }, [open, handleClick]);
  var iconButton = React.createElement(Button.Icon, {
    name: "search",
    type: "primary",
    color: "gray",
    disabled: disabled,
    className: "".concat(rootCls, "-icon"),
    onClick: handleClick
  });

  if (triggerTooltipProps) {
    iconButton = React.createElement(Tooltip, _extends({}, triggerTooltipProps, {
      trigger: iconButton
    }), triggerTooltipProps.children);
  }

  var animation = {
    enter: "".concat(rootCls, "-fade-in"),
    leave: "".concat(rootCls, "-fade-out")
  };
  return React.createElement("div", {
    className: wrapperCls
  }, React.createElement(Animate, {
    animation: animation
  }, open ? null : iconButton), React.createElement(Search, _extends({}, searchProps, {
    ref: searchRef,
    disabled: disabled,
    className: searchCls,
    onFocus: handleFocus,
    value: value,
    onBlur: handleBlur,
    onChange: handleChange,
    type: "normal"
  })));
});

var IconSearch =
/*#__PURE__*/
function (_React$Component) {
  _inherits(IconSearch, _React$Component);

  function IconSearch() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, IconSearch);

    for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
      args[_key2] = arguments[_key2];
    }

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(IconSearch)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_this), "state", {
      open: false
    });

    _defineProperty(_assertThisInitialized(_this), "handleOpenChange", function (open) {
      _this.setState({
        open: open
      });

      if (isFunction(_this.props.onOpenedChange)) {
        _this.props.onOpenedChange(open);
      }
    });

    return _this;
  }

  _createClass(IconSearch, [{
    key: "open",
    value: function open() {
      this.handleOpenChange(true);
    }
  }, {
    key: "close",
    value: function close() {
      this.handleOpenChange(false);
    }
  }, {
    key: "render",
    value: function render() {
      return React.createElement(IconSearchFC, _extends({}, this.props, {
        open: this.state.open,
        onOpen: this.handleOpenChange
      }));
    }
  }]);

  return IconSearch;
}(React.Component);

_defineProperty(IconSearch, "propTypes", {
  prefix: PropTypes.string,
  direction: PropTypes.oneOf(['ltr', 'rtl']),
  searchCls: PropTypes.string,
  triggerTooltipProps: PropTypes.object,
  onOpenedChange: PropTypes.func
});

_defineProperty(IconSearch, "defaultProps", {
  prefix: 'next-',
  direction: 'ltr'
});

export default ConfigProvider.config(IconSearch);