'use strict';

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

var vue = require('vue');
require('../../../../utils/index.cjs');
var useLock = require('../hooks/useLock.cjs');
var keyUtil = require('../utils/keyUtil.cjs');
var SingleSelector = require('./SingleSelector.cjs');
var MultipleSelector = require('./MultipleSelector.cjs');
var vueTypes = require('../../../../utils/vue/vueTypes.cjs');
var createRef = require('../../../../utils/vue/createRef.cjs');
var KeyCode = require('../../../../utils/KeyCode.cjs');

const Selector = /* @__PURE__ */ vue.defineComponent({
  name: "Selector",
  inheritAttrs: false,
  props: {
    id: String,
    prefixCls: String,
    showSearch: {
      type: Boolean,
      default: void 0
    },
    open: {
      type: Boolean,
      default: void 0
    },
    /** Display in the Selector value, it's not same as `value` prop */
    values: vueTypes.default.array,
    multiple: {
      type: Boolean,
      default: void 0
    },
    mode: String,
    searchValue: String,
    activeValue: String,
    inputElement: vueTypes.default.any,
    autofocus: {
      type: Boolean,
      default: void 0
    },
    activeDescendantId: String,
    tabindex: vueTypes.default.oneOfType([vueTypes.default.number, vueTypes.default.string]),
    disabled: {
      type: Boolean,
      default: void 0
    },
    placeholder: vueTypes.default.any,
    removeIcon: vueTypes.default.any,
    // Tags
    maxTagCount: vueTypes.default.oneOfType([vueTypes.default.number, vueTypes.default.string]),
    maxTagTextLength: Number,
    maxTagPlaceholder: vueTypes.default.any,
    tagRender: Function,
    optionLabelRender: Function,
    /** Check if `tokenSeparators` contains `\n` or `\r\n` */
    tokenWithEnter: {
      type: Boolean,
      default: void 0
    },
    // Motion
    choiceTransitionName: String,
    onToggleOpen: {
      type: Function
    },
    /** `onSearch` returns go next step boolean to check if need do toggle open */
    onSearch: Function,
    onSearchSubmit: Function,
    onRemove: Function,
    onInputKeyDown: {
      type: Function
    },
    /**
     * @private get real dom for trigger align.
     * This may be removed after React provides replacement of `findDOMNode`
     */
    domRef: Function
  },
  setup(props, {
    expose
  }) {
    const inputRef = createRef.createRef();
    let compositionStatus = false;
    const [getInputMouseDown, setInputMouseDown] = useLock.default(0);
    const onInternalInputKeyDown = (event) => {
      const {
        which
      } = event;
      if (which === KeyCode.KeyCode.UP || which === KeyCode.KeyCode.DOWN)
        event.preventDefault();
      if (props.onInputKeyDown)
        props.onInputKeyDown(event);
      if (which === KeyCode.KeyCode.ENTER && props.mode === "tags" && !compositionStatus && !props.open) {
        props.onSearchSubmit(event.target.value);
      }
      if (keyUtil.isValidateOpenKey(which))
        props.onToggleOpen(true);
    };
    const onInternalInputMouseDown = () => {
      setInputMouseDown(true);
    };
    let pastedText = null;
    const triggerOnSearch = (value) => {
      if (props.onSearch(value, true, compositionStatus) !== false)
        props.onToggleOpen(true);
    };
    const onInputCompositionStart = () => {
      compositionStatus = true;
    };
    const onInputCompositionEnd = (e) => {
      compositionStatus = false;
      if (props.mode !== "combobox")
        triggerOnSearch(e.target.value);
    };
    const onInputChange = (event) => {
      let {
        target: {
          value
        }
      } = event;
      if (props.tokenWithEnter && pastedText && /[\r\n]/.test(pastedText)) {
        const replacedText = pastedText.replace(/[\r\n]+$/, "").replace(/\r\n/g, " ").replace(/[\r\n]/g, " ");
        value = value.replace(replacedText, pastedText);
      }
      pastedText = null;
      triggerOnSearch(value);
    };
    const onInputPaste = (e) => {
      const {
        clipboardData
      } = e;
      const value = clipboardData.getData("text");
      pastedText = value;
    };
    const onClick = ({
      target
    }) => {
      if (target !== inputRef.current) {
        const isIE = document.body.style.msTouchAction !== void 0;
        if (isIE) {
          setTimeout(() => {
            inputRef.current.focus();
          });
        } else {
          inputRef.current.focus();
        }
      }
    };
    const onMousedown = (event) => {
      const inputMouseDown = getInputMouseDown();
      if (event.target !== inputRef.current && !inputMouseDown)
        event.preventDefault();
      if (props.mode !== "combobox" && (!props.showSearch || !inputMouseDown) || !props.open) {
        if (props.open)
          props.onSearch("", true, false);
        props.onToggleOpen();
      }
    };
    expose({
      focus: () => {
        inputRef.current.focus();
      },
      blur: () => {
        inputRef.current.blur();
      }
    });
    return () => {
      const {
        prefixCls,
        domRef,
        mode
      } = props;
      const sharedProps = {
        inputRef,
        onInputKeyDown: onInternalInputKeyDown,
        onInputMouseDown: onInternalInputMouseDown,
        onInputChange,
        onInputPaste,
        onInputCompositionStart,
        onInputCompositionEnd
      };
      const selectNode = mode === "multiple" || mode === "tags" ? vue.createVNode(MultipleSelector.default, {
        ...props,
        ...sharedProps
      }, null) : vue.createVNode(SingleSelector.default, {
        ...props,
        ...sharedProps
      }, null);
      return vue.createVNode("div", {
        "ref": domRef,
        "class": `${prefixCls}-selector`,
        "onClick": onClick,
        "onMousedown": onMousedown
      }, [selectNode]);
    };
  }
});

exports.default = Selector;
//# sourceMappingURL=index.cjs.map
