import React, { useRef, useContext, forwardRef, useImperativeHandle, useCallback } from 'react';
import { WauSearchOutlined } from '@wii/icons';
import { ConfigContext } from '../wau-config-provider';
import Input from '../input';
import './style';
import { jsx as _jsx } from "react/jsx-runtime";
const WauSearch = /*#__PURE__*/forwardRef(({
  onSearch: customOnSearch,
  onChange: customOnChange,
  iconProps,
  ...props
}, ref) => {
  const {
    getPrefixCls
  } = useContext(ConfigContext);
  const prefixCls = getPrefixCls('search');
  const inputRef = useRef(null);
  const onChange = useCallback(e => {
    if (e && e.target && e.type === 'click' && customOnSearch) {
      customOnSearch(e.target.value, e);
    }
    if (customOnChange) {
      customOnChange(e);
    }
  }, [customOnChange, customOnSearch]);
  const handlePressEnter = useCallback(e => {
    var _inputRef$current;
    customOnSearch === null || customOnSearch === void 0 ? void 0 : customOnSearch((_inputRef$current = inputRef.current) === null || _inputRef$current === void 0 || (_inputRef$current = _inputRef$current.input) === null || _inputRef$current === void 0 ? void 0 : _inputRef$current.value, e);
  }, [customOnSearch]);
  const handleSearchIconClick = useCallback(e => {
    var _inputRef$current2;
    customOnSearch === null || customOnSearch === void 0 ? void 0 : customOnSearch((_inputRef$current2 = inputRef.current) === null || _inputRef$current2 === void 0 || (_inputRef$current2 = _inputRef$current2.input) === null || _inputRef$current2 === void 0 ? void 0 : _inputRef$current2.value, e);
  }, [customOnSearch]);
  useImperativeHandle(ref, () => inputRef.current);
  return /*#__PURE__*/_jsx(Input, {
    type: "search",
    className: prefixCls,
    ref: inputRef,
    suffix: /*#__PURE__*/_jsx(WauSearchOutlined, {
      onClick: handleSearchIconClick,
      className: `${prefixCls}-icon`,
      ...iconProps
    }),
    onChange: onChange,
    onPressEnter: handlePressEnter,
    ...props
  });
});
export default WauSearch;