"use strict";

exports.__esModule = true;
exports.default = void 0;

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

var _clsx = _interopRequireDefault(require("clsx"));

var _icons = require("@react-vant/icons");

var _utils = require("../utils");

var _field = _interopRequireDefault(require("../field"));

var _ConfigProviderContext = _interopRequireDefault(require("../config-provider/ConfigProviderContext"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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

var Search = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
  var _useContext = (0, _react.useContext)(_ConfigProviderContext.default),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace,
      locale = _useContext.locale;

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

  var filedRef = (0, _react.useRef)();
  var innerEffect = (0, _react.useRef)(false);

  var _useState = (0, _react.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') {
      (0, _utils.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.default.createElement("div", {
        className: (0, _clsx.default)(bem('label'))
      }, props.label);
    }

    return null;
  };

  var renderAction = function renderAction() {
    if (props.showAction) {
      return /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _clsx.default)(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.default.createElement(_field.default, _extends({
      ref: filedRef,
      className: (0, _clsx.default)(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));
  };

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

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