import { defineComponent, ref, createVNode } from 'vue';
import { omit } from 'lodash';
import '../../../utils/index.mjs';
import { Select } from '../../select/index.mjs';
import useConfigInject from '../../config-provider/src/hooks/useConfigInject.mjs';
import Option from './Option.mjs';
import { autoCompleteProps } from './props.mjs';
import { devWarning } from '../../../utils/log.mjs';
import { flattenChildren } from '../../../utils/vue/props.mjs';
import { isValidElement } from '../../../utils/is.mjs';

function isSelectOptionOrSelectOptGroup(child) {
  var _a, _b;
  return ((_a = child == null ? void 0 : child.type) == null ? void 0 : _a.isSelectOption) || ((_b = child == null ? void 0 : child.type) == null ? void 0 : _b.isSelectOptGroup);
}
var AAutoComplete = /* @__PURE__ */ defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "AAutoComplete",
  inheritAttrs: false,
  props: autoCompleteProps(),
  // emits: ['change', 'select', 'focus', 'blur'],
  slots: Object,
  setup(props, {
    slots,
    attrs,
    expose
  }) {
    devWarning(!("dataSource" in slots), "AutoComplete", "`dataSource` slot is deprecated, please use props `options` instead.");
    devWarning(!("options" in slots), "AutoComplete", "`options` slot is deprecated, please use props `options` instead.");
    devWarning(!props.dropdownClassName, "AutoComplete", "`dropdownClassName` is deprecated, please use `popupClassName` instead.");
    const selectRef = ref();
    const getInputElement = () => {
      var _a;
      const children = flattenChildren((_a = slots.default) == null ? void 0 : _a.call(slots));
      const element = children.length ? children[0] : void 0;
      return element;
    };
    const focus = () => {
      var _a;
      (_a = selectRef.value) == null ? void 0 : _a.focus();
    };
    const blur = () => {
      var _a;
      (_a = selectRef.value) == null ? void 0 : _a.blur();
    };
    expose({
      focus,
      blur
    });
    const {
      prefixCls
    } = useConfigInject("select", props);
    return () => {
      var _a, _b, _c;
      const {
        size,
        dataSource,
        notFoundContent = (_a = slots.notFoundContent) == null ? void 0 : _a.call(slots)
      } = props;
      let optionChildren;
      const {
        class: className
      } = attrs;
      const cls = {
        [className]: !!className,
        [`${prefixCls.value}-lg`]: size === "large",
        [`${prefixCls.value}-sm`]: size === "small",
        [`${prefixCls.value}-show-search`]: true,
        [`${prefixCls.value}-auto-complete`]: true
      };
      if (props.options === void 0) {
        const childArray = ((_b = slots.dataSource) == null ? void 0 : _b.call(slots)) || ((_c = slots.options) == null ? void 0 : _c.call(slots)) || [];
        if (childArray.length && isSelectOptionOrSelectOptGroup(childArray[0])) {
          optionChildren = childArray;
        } else {
          optionChildren = dataSource ? dataSource.map((item) => {
            if (isValidElement(item))
              return item;
            switch (typeof item) {
              case "string":
                return createVNode(Option, {
                  "key": item,
                  "value": item
                }, {
                  default: () => [item]
                });
              case "object":
                return createVNode(Option, {
                  "key": item.value,
                  "value": item.value
                }, {
                  default: () => [item.text]
                });
              default:
                throw new Error("AutoComplete[dataSource] only supports type `string[] | Object[]`.");
            }
          }) : [];
        }
      }
      const selectProps = omit({
        ...props,
        ...attrs,
        mode: Select.SECRET_COMBOBOX_MODE_DO_NOT_USE,
        // optionLabelProp,
        getInputElement,
        notFoundContent,
        // placeholder: '',
        class: cls,
        popupClassName: props.popupClassName || props.dropdownClassName,
        ref: selectRef
      }, ["dataSource", "loading"]);
      return createVNode(Select, selectProps, {
        default: () => [optionChildren],
        ...omit(slots, ["default", "dataSource", "options"])
      });
    };
  }
});

export { AAutoComplete as default };
//# sourceMappingURL=AutoComplete.mjs.map
