import _extends from "@babel/runtime/helpers/extends";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";

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) { _defineProperty(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; }

import { createBEM } from '@zarm-design/bem';
import { Search as SearchIcon } from '@zarm-design/icons';
import isFunction from 'lodash/isFunction';
import * as React from 'react';
import { ConfigContext } from '../config-provider';
import Input from '../input';
import { useControllableEventValue } from '../utils/hooks';
import { resolveOnChange } from '../utils/resolveOnChange';
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 = _objectWithoutProperties(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 = useControllableEventValue(_objectSpread(_objectSpread({}, props), {}, {
    defaultValue: defaultValue
  })),
      _useControllableEvent2 = _slicedToArray(_useControllableEvent, 2),
      value = _useControllableEvent2[0],
      setValue = _useControllableEvent2[1];

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

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

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

    return showCancel && isFocus;
  }, [showCancel, isFocus]);
  var cls = bem([(_ref = {}, _defineProperty(_ref, "".concat(shape), !!shape), _defineProperty(_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;

    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(SearchIcon, {
    size: "sm"
  })), /*#__PURE__*/React.createElement(Input, _extends({
    ref: inputRef
  }, inputProps))), renderCancel()));
});
SearchBar.defaultProps = {
  shape: 'radius',
  clearable: true
};
SearchBar.displayName = 'SearchBar';
export default SearchBar;