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); }

import React, { forwardRef, useContext, useEffect, useImperativeHandle, useRef, useState } from 'react';
import clsx from 'clsx';
import { Search as SearchIco } from '@react-vant/icons';
import { preventDefault } from '../utils'; // Components

import Field from '../field';
import ConfigProviderContext from '../config-provider/ConfigProviderContext';
var Search = /*#__PURE__*/forwardRef(function (props, ref) {
  var _useContext = useContext(ConfigProviderContext),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace,
      locale = _useContext.locale;

  var _createNamespace = createNamespace('search', prefixCls),
      bem = _createNamespace[0];

  var filedRef = useRef();
  var innerEffect = useRef(false);

  var _useState = useState(function () {
    return props.value;
  }),
      value = _useState[0],
      setValue = _useState[1];

  var blur = function blur() {
    var _filedRef$current;

    (_filedRef$current = filedRef.current) == null ? void 0 : _filedRef$current.blur();
  };

  var focus = function focus() {
    var _filedRef$current2;

    (_filedRef$current2 = filedRef.current) == null ? void 0 : _filedRef$current2.focus();
  };

  var change = function change(v) {
    innerEffect.current = true;
    props == null ? void 0 : props.onChange == null ? void 0 : props.onChange(v);
    setValue(v);
  };

  var onCancel = function onCancel() {
    if (!props.actionText) {
      change('');
      props == null ? void 0 : props.onCancel == null ? void 0 : props.onCancel();
    }
  };

  var onKeypress = function onKeypress(event) {
    if (event.key === 'Enter') {
      preventDefault(event.nativeEvent);
      props == null ? void 0 : props.onSearch == null ? void 0 : props.onSearch(value);
    }
  };

  var onClear = function onClear(event) {
    change('');
    props == null ? void 0 : props.onClear == null ? void 0 : props.onClear(event);
  };

  var onClickInput = function onClickInput(event) {
    props == null ? void 0 : props.onClickInput == null ? void 0 : props.onClickInput(event);
  };

  var renderLabel = function renderLabel() {
    if (props.label) {
      return /*#__PURE__*/React.createElement("div", {
        className: clsx(bem('label'))
      }, props.label);
    }

    return null;
  };

  var renderAction = function renderAction() {
    if (props.showAction) {
      return /*#__PURE__*/React.createElement("div", {
        className: clsx(bem('action')),
        role: "button",
        tabIndex: 0,
        onClick: onCancel
      }, props.actionText || locale.cancel);
    }

    return null;
  };

  var _props$disabled = props.disabled,
      disabled = _props$disabled === void 0 ? false : _props$disabled,
      maxlength = props.maxlength,
      leftIcon = props.leftIcon,
      rightIcon = props.rightIcon,
      _props$clearable = props.clearable,
      clearable = _props$clearable === void 0 ? true : _props$clearable,
      clearTrigger = props.clearTrigger,
      placeholder = props.placeholder,
      inputAlign = props.inputAlign,
      readonly = props.readonly,
      error = props.error,
      errorMessage = props.errorMessage,
      formatter = props.formatter,
      formatTrigger = props.formatTrigger,
      clearIcon = props.clearIcon;
  var fieldPropNames = {
    leftIcon: leftIcon,
    rightIcon: rightIcon,
    clearable: clearable,
    clearTrigger: clearTrigger,
    placeholder: placeholder,
    disabled: disabled,
    maxlength: maxlength,
    inputAlign: inputAlign,
    readonly: readonly,
    error: error,
    errorMessage: errorMessage,
    formatter: formatter,
    formatTrigger: formatTrigger,
    clearIcon: clearIcon
  };

  var renderField = function renderField() {
    return /*#__PURE__*/React.createElement(Field, _extends({
      ref: filedRef,
      className: clsx(bem('field')),
      type: "search",
      rows: 1,
      value: value || '',
      border: false,
      onKeypress: onKeypress,
      onFocus: props.onFocus,
      onBlur: props.onBlur,
      onChange: change,
      onClear: onClear,
      onClickInput: onClickInput
    }, fieldPropNames));
  };

  useEffect(function () {
    if (innerEffect.current) {
      innerEffect.current = false;
      return;
    }

    setValue(props.value);
  }, [props.value]);
  useImperativeHandle(ref, function () {
    return {
      focus: focus,
      blur: blur
    };
  });
  return /*#__PURE__*/React.createElement("div", {
    className: clsx(props.className, bem({
      'show-action': props.showAction
    })),
    style: _extends({}, props.style, {
      background: props.background
    })
  }, /*#__PURE__*/React.createElement("div", {
    className: clsx(bem('content', props.shape))
  }, renderLabel(), renderField()), renderAction());
});
Search.defaultProps = {
  shape: 'square',
  leftIcon: /*#__PURE__*/React.createElement(SearchIco, null),
  inputAlign: 'left'
};
export default Search;