"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const event = require("../../../constants/event.js");
const shared_esmBundler = require("../../../../../@vue/shared/dist/shared.esm-bundler.js");
const vnode = require("../../../utils/vue/vnode.js");
const index$5 = require("../../../hooks/use-calc-input-width/index.js");
const index$1 = require("../../icon/index.js");
const index$2 = require("../../tooltip/index.js");
const index$3 = require("../../scrollbar/index.js");
const index = require("../../../directives/click-outside/index.js");
const index$4 = require("../../tag/index.js");
const useProps = require("../../select-v2/src/useProps.js");
const token = require("./token.js");
const option2 = require("./option2.js");
const selectDropdown = require("./select-dropdown.js");
const useSelect = require("./useSelect.js");
const options = require("./options.js");
const select = require("./select.js");
const optionGroup = require("./option-group.js");
const vue = require("vue");
const COMPONENT_NAME = "ElSelect";
const warnHandlerMap = /* @__PURE__ */ new WeakMap();
const createSelectWarnHandler = (appContext) => {
  return (...args) => {
    var _a, _b;
    const message = args[0];
    if (!message || message.includes('Slot "default" invoked outside of the render function') && ((_a = args[2]) == null ? void 0 : _a.includes("ElTreeSelect"))) return;
    const original = (_b = warnHandlerMap.get(appContext)) == null ? void 0 : _b.originalWarnHandler;
    if (original) {
      original(...args);
      return;
    }
    console.warn(...args);
  };
};
const getWarnHandlerRecord = (appContext) => {
  let record = warnHandlerMap.get(appContext);
  if (!record) {
    record = {
      originalWarnHandler: appContext.config.warnHandler,
      handler: createSelectWarnHandler(appContext),
      count: 0
    };
    warnHandlerMap.set(appContext, record);
  }
  return record;
};
var select_vue_vue_type_script_lang_default = vue.defineComponent({
  name: COMPONENT_NAME,
  componentName: COMPONENT_NAME,
  components: {
    ElSelectMenu: selectDropdown.default,
    ElOption: option2.default,
    ElOptions: options.default,
    ElOptionGroup: optionGroup.default,
    ElTag: index$4.ElTag,
    ElScrollbar: index$3.ElScrollbar,
    ElTooltip: index$2.ElTooltip,
    ElIcon: index$1.ElIcon
  },
  directives: { ClickOutside: index.default },
  props: select.selectProps,
  emits: [
    event.UPDATE_MODEL_EVENT,
    event.CHANGE_EVENT,
    "remove-tag",
    "clear",
    "visible-change",
    "focus",
    "blur",
    "popup-scroll"
  ],
  setup(props, { emit, slots }) {
    const instance = vue.getCurrentInstance();
    const warnRecord = getWarnHandlerRecord(instance.appContext);
    warnRecord.count += 1;
    instance.appContext.config.warnHandler = warnRecord.handler;
    const modelValue = vue.computed(() => {
      const { modelValue: rawModelValue, multiple } = props;
      const fallback = multiple ? [] : void 0;
      if (shared_esmBundler.isArray(rawModelValue)) return multiple ? rawModelValue : fallback;
      return multiple ? fallback : rawModelValue;
    });
    const _props = vue.reactive({
      ...vue.toRefs(props),
      modelValue
    });
    const API = useSelect.useSelect(_props, emit);
    const { calculatorRef, inputStyle } = index$5.useCalcInputWidth();
    const { getLabel, getValue, getOptions, getDisabled } = useProps.useProps(props);
    const getOptionProps = (option) => ({
      label: getLabel(option),
      value: getValue(option),
      disabled: getDisabled(option)
    });
    const flatTreeSelectData = (data) => {
      return data.reduce((acc, item) => {
        acc.push(item);
        if (item.children && item.children.length > 0) acc.push(...flatTreeSelectData(item.children));
        return acc;
      }, []);
    };
    const manuallyRenderSlots = (vnodes) => {
      vnode.flattedChildren(vnodes || []).forEach((item) => {
        var _a;
        if (shared_esmBundler.isObject(item) && (item.type.name === "ElOption" || item.type.name === "ElTree")) {
          const _name = item.type.name;
          if (_name === "ElTree") flatTreeSelectData(((_a = item.props) == null ? void 0 : _a.data) || []).forEach((treeItem) => {
            treeItem.currentLabel = treeItem.label ?? (shared_esmBundler.isObject(treeItem.value) ? "" : treeItem.value);
            API.onOptionCreate(treeItem);
          });
          else if (_name === "ElOption") {
            const obj = { ...item.props };
            obj.currentLabel = obj.label ?? (shared_esmBundler.isObject(obj.value) ? "" : obj.value);
            API.onOptionCreate(obj);
          }
        }
      });
    };
    vue.watch(() => {
      var _a;
      return [props.persistent || API.expanded.value || !slots.default ? void 0 : (_a = slots.default) == null ? void 0 : _a.call(slots), modelValue.value];
    }, () => {
      var _a;
      if (props.persistent || API.expanded.value) return;
      if (!slots.default) return;
      API.states.options.clear();
      manuallyRenderSlots((_a = slots.default) == null ? void 0 : _a.call(slots));
    }, { immediate: true });
    vue.provide(token.selectKey, vue.reactive({
      props: _props,
      states: API.states,
      selectRef: API.selectRef,
      optionsArray: API.optionsArray,
      setSelected: API.setSelected,
      handleOptionSelect: API.handleOptionSelect,
      onOptionCreate: API.onOptionCreate,
      onOptionDestroy: API.onOptionDestroy
    }));
    const selectedLabel = vue.computed(() => {
      if (!props.multiple) return API.states.selectedLabel;
      return API.states.selected.map((i) => i.currentLabel);
    });
    vue.onBeforeUnmount(() => {
      const record = warnHandlerMap.get(instance.appContext);
      if (!record) return;
      record.count -= 1;
      if (record.count <= 0) {
        instance.appContext.config.warnHandler = record.originalWarnHandler;
        warnHandlerMap.delete(instance.appContext);
      }
    });
    return {
      ...API,
      modelValue,
      selectedLabel,
      calculatorRef,
      inputStyle,
      getLabel,
      getValue,
      getOptions,
      getDisabled,
      getOptionProps
    };
  }
});
exports.default = select_vue_vue_type_script_lang_default;
