'use strict';

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

var vue = require('vue');
var pickAttrs = require('../../_util/pickAttrs.js');
var index = require('../../_util/vue-types/index.js');
var LegacyContext = require('../../vc-tree-select/LegacyContext.js');
var Input = require('./Input.js');

const props = {
  inputElement: index.default.any,
  id: String,
  prefixCls: String,
  values: index.default.array,
  open: {
    type: Boolean,
    default: void 0
  },
  searchValue: String,
  inputRef: index.default.any,
  placeholder: index.default.any,
  disabled: {
    type: Boolean,
    default: void 0
  },
  mode: String,
  showSearch: {
    type: Boolean,
    default: void 0
  },
  autofocus: {
    type: Boolean,
    default: void 0
  },
  autocomplete: String,
  activeDescendantId: String,
  tabindex: index.default.oneOfType([index.default.number, index.default.string]),
  activeValue: String,
  backfill: {
    type: Boolean,
    default: void 0
  },
  optionLabelRender: Function,
  onInputChange: Function,
  onInputPaste: Function,
  onInputKeyDown: Function,
  onInputMouseDown: Function,
  onInputCompositionStart: Function,
  onInputCompositionEnd: Function
};
const SingleSelector = /* @__PURE__ */ vue.defineComponent({
  name: "SingleSelector",
  setup(props2) {
    const inputChanged = vue.shallowRef(false);
    const combobox = vue.computed(() => props2.mode === "combobox");
    const inputEditable = vue.computed(() => combobox.value || props2.showSearch);
    const inputValue = vue.computed(() => {
      let inputValue2 = props2.searchValue || "";
      if (combobox.value && props2.activeValue && !inputChanged.value)
        inputValue2 = props2.activeValue;
      return inputValue2;
    });
    const legacyTreeSelectContext = LegacyContext.default();
    vue.watch([combobox, () => props2.activeValue], () => {
      if (combobox.value)
        inputChanged.value = false;
    }, {
      immediate: true
    });
    const hasTextInput = vue.computed(() => props2.mode !== "combobox" && !props2.open && !props2.showSearch ? false : !!inputValue.value);
    const title = vue.computed(() => {
      const item = props2.values[0];
      return item && (typeof item.label === "string" || typeof item.label === "number") ? item.label.toString() : void 0;
    });
    const renderPlaceholder = () => {
      if (props2.values[0])
        return null;
      const hiddenStyle = hasTextInput.value ? {
        visibility: "hidden"
      } : void 0;
      return vue.createVNode("span", {
        "class": `${props2.prefixCls}-selection-placeholder`,
        "style": hiddenStyle
      }, [props2.placeholder]);
    };
    return () => {
      var _a, _b, _c, _d;
      const {
        inputElement,
        prefixCls,
        id,
        values,
        inputRef,
        disabled,
        autofocus,
        autocomplete,
        activeDescendantId,
        open,
        tabindex,
        optionLabelRender,
        onInputKeyDown,
        onInputMouseDown,
        onInputChange,
        onInputPaste,
        onInputCompositionStart,
        onInputCompositionEnd
      } = props2;
      const item = values[0];
      let titleNode = null;
      if (item && legacyTreeSelectContext.customSlots) {
        const key = (_a = item.key) != null ? _a : item.value;
        const originData = ((_b = legacyTreeSelectContext.keyEntities[key]) == null ? void 0 : _b.node) || {};
        titleNode = legacyTreeSelectContext.customSlots[(_c = originData.slots) == null ? void 0 : _c.title] || legacyTreeSelectContext.customSlots.title || item.label;
        if (typeof titleNode === "function")
          titleNode = titleNode(originData);
      } else {
        titleNode = optionLabelRender && item ? optionLabelRender(item.option) : item == null ? void 0 : item.label;
      }
      return vue.createVNode(vue.Fragment, null, [vue.createVNode("span", {
        "class": `${prefixCls}-selection-search`
      }, [vue.createVNode(Input.default, {
        "inputRef": inputRef,
        "prefixCls": prefixCls,
        "id": id,
        "open": open,
        "inputElement": inputElement,
        "disabled": disabled,
        "autofocus": autofocus,
        "autocomplete": autocomplete,
        "editable": inputEditable.value,
        "activeDescendantId": activeDescendantId,
        "value": inputValue.value,
        "onKeydown": onInputKeyDown,
        "onMousedown": onInputMouseDown,
        "onChange": (e) => {
          inputChanged.value = true;
          onInputChange(e);
        },
        "onPaste": onInputPaste,
        "onCompositionstart": onInputCompositionStart,
        "onCompositionend": onInputCompositionEnd,
        "tabindex": tabindex,
        "attrs": pickAttrs.default(props2, true)
      }, null)]), !combobox.value && item && !hasTextInput.value && vue.createVNode("span", {
        "class": `${prefixCls}-selection-item`,
        "title": title.value
      }, [vue.createVNode(vue.Fragment, {
        "key": (_d = item.key) != null ? _d : item.value
      }, [titleNode])]), renderPlaceholder()]);
    };
  }
});
SingleSelector.props = props;
SingleSelector.inheritAttrs = false;

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