'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var vue = require('vue');
var CloseOutlined = require('@ant-design/icons-vue');
var isPlainObject = require('lodash-es/isPlainObject');
require('../button/index.js');
var vnode = require('../_util/vnode.js');
var index = require('../_util/vue-types/index.js');
var classNames = require('../_util/classNames.js');
require('../hooks/index.js');
var omit = require('../_util/omit.js');
var inputProps = require('./inputProps.js');
var Input = require('./Input.js');
var useConfigInject = require('../config-provider/hooks/useConfigInject.js');
var button = require('../button/button.js');

var Search = /* @__PURE__ */ vue.defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "AInputSearch",
  inheritAttrs: false,
  props: {
    ...inputProps.default(),
    inputPrefixCls: String,
    // 不能设置默认值 https://github.com/vueComponent/ant-design-vue/issues/1916
    enterButton: index.default.any,
    onSearch: {
      type: Function
    }
  },
  setup(props, {
    slots,
    attrs,
    expose,
    emit
  }) {
    const inputRef = vue.shallowRef();
    const composedRef = vue.shallowRef(false);
    const focus = () => {
      var _a;
      (_a = inputRef.value) == null ? void 0 : _a.focus();
    };
    const blur = () => {
      var _a;
      (_a = inputRef.value) == null ? void 0 : _a.blur();
    };
    expose({
      focus,
      blur
    });
    const onChange = (e) => {
      emit("update:value", e.target.value);
      if (e && e.target && e.type === "click")
        emit("search", e.target.value, e);
      emit("change", e);
    };
    const onMousedown = (e) => {
      var _a;
      if (document.activeElement === ((_a = inputRef.value) == null ? void 0 : _a.input))
        e.preventDefault();
    };
    const onSearch = (e) => {
      var _a, _b;
      emit("search", (_b = (_a = inputRef.value) == null ? void 0 : _a.input) == null ? void 0 : _b.stateValue, e);
    };
    const onPressEnter = (e) => {
      if (composedRef.value || props.loading)
        return;
      onSearch(e);
    };
    const handleOnCompositionStart = (e) => {
      composedRef.value = true;
      emit("compositionstart", e);
    };
    const handleOnCompositionEnd = (e) => {
      composedRef.value = false;
      emit("compositionend", e);
    };
    const {
      prefixCls,
      getPrefixCls,
      direction,
      size
    } = useConfigInject.default("input-search", props);
    const inputPrefixCls = vue.computed(() => getPrefixCls("input", props.inputPrefixCls));
    return () => {
      var _a, _b, _c, _d;
      const {
        disabled,
        loading,
        addonAfter = (_a = slots.addonAfter) == null ? void 0 : _a.call(slots),
        suffix = (_b = slots.suffix) == null ? void 0 : _b.call(slots),
        ...restProps
      } = props;
      let {
        enterButton = (_d = (_c = slots.enterButton) == null ? void 0 : _c.call(slots)) != null ? _d : false
      } = props;
      enterButton = enterButton || enterButton === "";
      const searchIcon = typeof enterButton === "boolean" ? vue.createVNode(CloseOutlined.SearchOutlined, null, null) : null;
      const btnClassName = `${prefixCls.value}-button`;
      const enterButtonAsElement = Array.isArray(enterButton) ? enterButton[0] : enterButton;
      let button$1;
      const isAntdButton = enterButtonAsElement.type && isPlainObject(enterButtonAsElement.type) && enterButtonAsElement.type.__ANT_BUTTON;
      if (isAntdButton || enterButtonAsElement.tagName === "button") {
        button$1 = vnode.cloneElement(enterButtonAsElement, {
          onMousedown,
          onClick: onSearch,
          key: "enterButton",
          ...isAntdButton ? {
            class: btnClassName,
            size: size.value
          } : {}
        }, false);
      } else {
        const iconOnly = searchIcon && !enterButton;
        button$1 = vue.createVNode(button.default, {
          "class": btnClassName,
          "type": enterButton ? "primary" : void 0,
          "size": size.value,
          "disabled": disabled,
          "key": "enterButton",
          "onMousedown": onMousedown,
          "onClick": onSearch,
          "loading": loading,
          "icon": iconOnly ? searchIcon : null
        }, {
          default: () => [iconOnly ? null : searchIcon || enterButton]
        });
      }
      if (addonAfter)
        button$1 = [button$1, addonAfter];
      const cls = classNames.default(prefixCls.value, {
        [`${prefixCls.value}-rtl`]: direction.value === "rtl",
        [`${prefixCls.value}-${size.value}`]: !!size.value,
        [`${prefixCls.value}-with-button`]: !!enterButton
      }, attrs.class);
      return vue.createVNode(Input.default, vue.mergeProps({
        "ref": inputRef
      }, omit.default(restProps, ["onUpdate:value", "onSearch", "enterButton"]), attrs, {
        "onPressEnter": onPressEnter,
        "onCompositionstart": handleOnCompositionStart,
        "onCompositionend": handleOnCompositionEnd,
        "size": size.value,
        "prefixCls": inputPrefixCls.value,
        "addonAfter": button$1,
        "suffix": suffix,
        "onChange": onChange,
        "class": cls,
        "disabled": disabled
      }), slots);
    };
  }
});

exports.default = Search;
//# sourceMappingURL=Search.js.map
