import { isVNode, defineComponent, ref, createVNode } from 'vue';
import '../select/index.mjs';
import '../hooks/index.mjs';
import omit from '../_util/omit.mjs';
import warning from '../_util/warning.mjs';
import { flattenChildren, isValidElement } from '../_util/props-util/index.mjs';
import Option from './Option.mjs';
import OptGroup from './OptGroup.mjs';
import { selectProps } from '../select/props.mjs';
import useConfigInject from '../config-provider/hooks/useConfigInject.mjs';
import Select from '../select/select.mjs';

function _isSlot(s) {
  return typeof s === "function" || Object.prototype.toString.call(s) === "[object Object]" && !isVNode(s);
}
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);
}
const autoCompleteProps = () => ({
  ...omit(selectProps(), ["loading", "mode", "optionLabelProp", "labelInValue"]),
  dataSource: Array,
  dropdownMenuStyle: {
    type: Object,
    default: void 0
  },
  // optionLabelProp: String,
  dropdownMatchSelectWidth: {
    type: [Number, Boolean],
    default: true
  },
  prefixCls: String,
  showSearch: {
    type: Boolean,
    default: void 0
  },
  transitionName: String,
  choiceTransitionName: {
    type: String,
    default: "zoom"
  },
  autofocus: {
    type: Boolean,
    default: void 0
  },
  backfill: {
    type: Boolean,
    default: void 0
  },
  // optionLabelProp: PropTypes.string.def('children'),
  filterOption: {
    type: [Boolean, Function],
    default: false
  },
  defaultActiveFirstOption: {
    type: Boolean,
    default: true
  },
  status: String
});
const AutoCompleteOption = Option;
const AutoCompleteOptGroup = OptGroup;
const AutoComplete = /* @__PURE__ */ defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "AAutoComplete",
  inheritAttrs: false,
  props: autoCompleteProps(),
  // emits: ['change', 'select', 'focus', 'blur'],
  slots: Object,
  setup(props, {
    slots,
    attrs,
    expose
  }) {
    warning(!("dataSource" in slots), "AutoComplete", "`dataSource` slot is deprecated, please use props `options` instead.");
    warning(!("options" in slots), "AutoComplete", "`options` slot is deprecated, please use props `options` instead.");
    warning(!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
                }, _isSlot(item) ? 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 selectProps2 = 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, selectProps2, {
        default: () => [optionChildren],
        ...omit(slots, ["default", "dataSource", "options"])
      });
    };
  }
});
var index = Object.assign(AutoComplete, {
  Option,
  OptGroup,
  install(app) {
    app.component(AutoComplete.name, AutoComplete);
    app.component(Option.displayName, Option);
    app.component(OptGroup.displayName, OptGroup);
    return app;
  }
});

export { AutoCompleteOptGroup, AutoCompleteOption, autoCompleteProps, index as default };
//# sourceMappingURL=index.mjs.map
