"use strict";

var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");

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

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

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

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

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

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

var _bem = require("@zarm-design/bem");

var _icons = require("@zarm-design/icons");

var _isFunction = _interopRequireDefault(require("lodash/isFunction"));

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

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

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

var _hooks = require("../utils/hooks");

var _resolveOnChange = require("../utils/resolveOnChange");

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

var SearchBar = /*#__PURE__*/React.forwardRef(function (props, ref) {
  var _ref;

  var className = props.className,
      style = props.style,
      shape = props.shape,
      icon = props.icon,
      placeholder = props.placeholder,
      showCancel = props.showCancel,
      cancelText = props.cancelText,
      _props$defaultValue = props.defaultValue,
      defaultValue = _props$defaultValue === void 0 ? '' : _props$defaultValue,
      onFocus = props.onFocus,
      onBlur = props.onBlur,
      onChange = props.onChange,
      onCancel = props.onCancel,
      onSubmit = props.onSubmit,
      restProps = (0, _objectWithoutProperties2.default)(props, ["className", "style", "shape", "icon", "placeholder", "showCancel", "cancelText", "defaultValue", "onFocus", "onBlur", "onChange", "onCancel", "onSubmit"]);
  var cancelRef = React.useRef(null);
  var inputRef = React.useRef();
  var formRef = /*#__PURE__*/React.createRef();

  var _useControllableEvent = (0, _hooks.useControllableEventValue)(_objectSpread(_objectSpread({}, props), {}, {
    defaultValue: defaultValue
  })),
      _useControllableEvent2 = (0, _slicedToArray2.default)(_useControllableEvent, 2),
      value = _useControllableEvent2[0],
      setValue = _useControllableEvent2[1];

  var _React$useState = React.useState(false),
      _React$useState2 = (0, _slicedToArray2.default)(_React$useState, 2),
      isFocus = _React$useState2[0],
      setIsFocus = _React$useState2[1];

  var _React$useContext = React.useContext(_configProvider.ConfigContext),
      prefixCls = _React$useContext.prefixCls,
      globalLocal = _React$useContext.locale;

  var bem = (0, _bem.createBEM)('search-bar', {
    prefixCls: prefixCls
  });
  var locale = globalLocal === null || globalLocal === void 0 ? void 0 : globalLocal.SearchBar;
  var isShowCancel = React.useMemo(function () {
    if ((0, _isFunction.default)(showCancel)) {
      return showCancel(isFocus, value);
    }

    return showCancel && isFocus;
  }, [showCancel, isFocus]);
  var cls = bem([(_ref = {}, (0, _defineProperty2.default)(_ref, "".concat(shape), !!shape), (0, _defineProperty2.default)(_ref, "focus", isFocus), _ref), className]);

  var onInputFocus = function onInputFocus(e) {
    setIsFocus(true);
    onFocus === null || onFocus === void 0 ? void 0 : onFocus(e);
  };

  var onInputBlur = function onInputBlur(e) {
    setIsFocus(false);
    onBlur === null || onBlur === void 0 ? void 0 : onBlur(e);
  };

  var onInputChange = function onInputChange(e) {
    setValue === null || setValue === void 0 ? void 0 : setValue(e);
  };

  var onFormSubmit = function onFormSubmit(e) {
    e === null || e === void 0 ? void 0 : e.preventDefault();
    inputRef.current && inputRef.current.blur();
    onSubmit === null || onSubmit === void 0 ? void 0 : onSubmit(value);
  };

  var onClickCancelButton = function onClickCancelButton(e) {
    var _inputRef$current;

    (0, _resolveOnChange.resolveOnChange)((_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 ? void 0 : _inputRef$current.nativeElement, e, setValue);
    inputRef.current && inputRef.current.blur();
    onCancel === null || onCancel === void 0 ? void 0 : onCancel();
  };

  React.useImperativeHandle(ref, function () {
    var _inputRef$current2, _inputRef$current3;

    return {
      focus: (_inputRef$current2 = inputRef.current) === null || _inputRef$current2 === void 0 ? void 0 : _inputRef$current2.focus,
      blur: (_inputRef$current3 = inputRef.current) === null || _inputRef$current3 === void 0 ? void 0 : _inputRef$current3.blur,
      submit: onFormSubmit
    };
  });

  var renderCancel = function renderCancel() {
    return isShowCancel && /*#__PURE__*/React.createElement("div", {
      className: bem('cancel'),
      ref: cancelRef,
      onClick: onClickCancelButton
    }, cancelText || (locale === null || locale === void 0 ? void 0 : locale.cancelText));
  };

  var inputProps = _objectSpread({
    type: 'search',
    placeholder: placeholder || locale && locale.placeholder,
    value: value,
    onFocus: onInputFocus,
    onBlur: onInputBlur,
    onChange: onInputChange
  }, restProps);

  return /*#__PURE__*/React.createElement("div", {
    className: cls,
    style: style
  }, /*#__PURE__*/React.createElement("form", {
    action: "#",
    className: bem('form'),
    onSubmit: onFormSubmit,
    ref: formRef
  }, /*#__PURE__*/React.createElement("div", {
    className: bem('content')
  }, icon !== null && /*#__PURE__*/React.createElement("div", {
    className: bem('icon')
  }, icon || /*#__PURE__*/React.createElement(_icons.Search, {
    size: "sm"
  })), /*#__PURE__*/React.createElement(_input.default, (0, _extends2.default)({
    ref: inputRef
  }, inputProps))), renderCancel()));
});
SearchBar.defaultProps = {
  shape: 'radius',
  clearable: true
};
SearchBar.displayName = 'SearchBar';
var _default = SearchBar;
exports.default = _default;