"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const aria = require("../../../../constants/aria.js");
const event = require("../../../../constants/event.js");
const browser = require("../../../../utils/browser.js");
const event$1 = require("../../../../utils/dom/event.js");
const types = require("../../../../utils/types.js");
const error = require("../../../../utils/error.js");
const index = require("../../../../hooks/use-focus-controller/index.js");
const index$1 = require("../../../../hooks/use-composition/index.js");
const useFormCommonProps = require("../../../form/src/hooks/use-form-common-props.js");
const Vue = require("vue");
const castArray = require("../../../../../../lodash-es/castArray.js");
function useInputTag({ props, emit, formItem }) {
  const disabled = useFormCommonProps.useFormDisabled();
  const size = useFormCommonProps.useFormSize();
  const inputRef = Vue.shallowRef();
  const inputValue = Vue.ref();
  const tagTooltipRef = Vue.ref();
  const tagSize = Vue.computed(() => {
    return ["small"].includes(size.value) ? "small" : "default";
  });
  const placeholder = Vue.computed(() => {
    var _a;
    return ((_a = props.modelValue) == null ? void 0 : _a.length) ? void 0 : props.placeholder;
  });
  const closable = Vue.computed(() => !(props.readonly || disabled.value));
  const inputLimit = Vue.computed(() => {
    var _a;
    return types.isUndefined(props.max) ? false : (((_a = props.modelValue) == null ? void 0 : _a.length) ?? 0) >= props.max;
  });
  const showTagList = Vue.computed(() => {
    var _a;
    return props.collapseTags ? (_a = props.modelValue) == null ? void 0 : _a.slice(0, props.maxCollapseTags) : props.modelValue;
  });
  const collapseTagList = Vue.computed(() => {
    var _a;
    return props.collapseTags ? (_a = props.modelValue) == null ? void 0 : _a.slice(props.maxCollapseTags) : [];
  });
  const addTagsEmit = (value) => {
    const list = [...props.modelValue ?? [], ...castArray.default(value)];
    emit(event.UPDATE_MODEL_EVENT, list);
    emit(event.CHANGE_EVENT, list);
    emit("add-tag", value);
    inputValue.value = void 0;
  };
  const getDelimitedTags = (input) => {
    var _a;
    const parts = input.split(props.delimiter);
    const tags = parts.length > 1 ? parts.map((val) => val.trim()).filter(Boolean) : [];
    if (props.max) {
      const maxInsert = props.max - (((_a = props.modelValue) == null ? void 0 : _a.length) ?? 0);
      tags.splice(maxInsert);
    }
    return tags.length === 1 ? tags[0] : tags;
  };
  const handlePaste = (event$12) => {
    var _a;
    const pasted = (_a = event$12.clipboardData) == null ? void 0 : _a.getData("text");
    if (props.readonly || inputLimit.value || !props.delimiter || !pasted) return;
    const { selectionStart = 0, selectionEnd = 0, value } = event$12.target;
    const nextValue = value.slice(0, selectionStart) + pasted + value.slice(selectionEnd);
    const tags = getDelimitedTags(nextValue);
    if (tags.length) {
      addTagsEmit(tags);
      emit(event.INPUT_EVENT, nextValue);
      event$12.preventDefault();
    }
  };
  const handleInput = (event$12) => {
    if (inputLimit.value) {
      inputValue.value = void 0;
      return;
    }
    if (isComposing.value) return;
    if (props.delimiter && inputValue.value) {
      const tags = getDelimitedTags(inputValue.value);
      if (tags.length) addTagsEmit(tags);
    }
    emit(event.INPUT_EVENT, event$12.target.value);
  };
  const handleKeydown = (event2) => {
    var _a;
    if (isComposing.value) return;
    switch (event$1.getEventCode(event2)) {
      case props.trigger:
        event2.preventDefault();
        event2.stopPropagation();
        handleAddTag();
        break;
      case aria.EVENT_CODE.numpadEnter:
        if (props.trigger === aria.EVENT_CODE.enter) {
          event2.preventDefault();
          event2.stopPropagation();
          handleAddTag();
        }
        break;
      case aria.EVENT_CODE.backspace:
        if (!inputValue.value && ((_a = props.modelValue) == null ? void 0 : _a.length)) {
          event2.preventDefault();
          event2.stopPropagation();
          handleRemoveTag(props.modelValue.length - 1);
        }
        break;
    }
  };
  const handleKeyup = (event2) => {
    if (isComposing.value || !browser.isAndroid()) return;
    switch (event$1.getEventCode(event2)) {
      case aria.EVENT_CODE.space:
        if (props.trigger === aria.EVENT_CODE.space) {
          event2.preventDefault();
          event2.stopPropagation();
          handleAddTag();
        }
        break;
    }
  };
  const handleAddTag = () => {
    var _a;
    const value = (_a = inputValue.value) == null ? void 0 : _a.trim();
    if (!value || inputLimit.value) return;
    addTagsEmit(value);
  };
  const handleRemoveTag = (index2) => {
    const value = (props.modelValue ?? []).slice();
    const [item] = value.splice(index2, 1);
    emit(event.UPDATE_MODEL_EVENT, value);
    emit(event.CHANGE_EVENT, value);
    emit("remove-tag", item, index2);
  };
  const handleClear = () => {
    inputValue.value = void 0;
    emit(event.UPDATE_MODEL_EVENT, void 0);
    emit(event.CHANGE_EVENT, void 0);
    emit("clear");
  };
  const handleDragged = (draggingIndex, dropIndex, type) => {
    const value = (props.modelValue ?? []).slice();
    const [draggedItem] = value.splice(draggingIndex, 1);
    const step = dropIndex > draggingIndex && type === "before" ? -1 : dropIndex < draggingIndex && type === "after" ? 1 : 0;
    value.splice(dropIndex + step, 0, draggedItem);
    emit(event.UPDATE_MODEL_EVENT, value);
    emit(event.CHANGE_EVENT, value);
    emit("drag-tag", draggingIndex, dropIndex + step, draggedItem);
  };
  const focus = () => {
    var _a;
    (_a = inputRef.value) == null ? void 0 : _a.focus();
  };
  const blur = () => {
    var _a;
    (_a = inputRef.value) == null ? void 0 : _a.blur();
  };
  const { wrapperRef, isFocused } = index.useFocusController(inputRef, {
    disabled,
    beforeBlur(event2) {
      var _a;
      return (_a = tagTooltipRef.value) == null ? void 0 : _a.isFocusInsideContent(event2);
    },
    afterBlur() {
      var _a;
      if (props.saveOnBlur) handleAddTag();
      else inputValue.value = void 0;
      if (props.validateEvent) (_a = formItem == null ? void 0 : formItem.validate) == null ? void 0 : _a.call(formItem, "blur").catch((err) => error.debugWarn(err));
    }
  });
  const { isComposing, handleCompositionStart, handleCompositionUpdate, handleCompositionEnd } = index$1.useComposition({ afterComposition: handleInput });
  Vue.watch(() => props.modelValue, () => {
    var _a;
    if (props.validateEvent) (_a = formItem == null ? void 0 : formItem.validate) == null ? void 0 : _a.call(formItem, event.CHANGE_EVENT).catch((err) => error.debugWarn(err));
  });
  return {
    inputRef,
    wrapperRef,
    tagTooltipRef,
    isFocused,
    isComposing,
    inputValue,
    size,
    tagSize,
    placeholder,
    closable,
    disabled,
    inputLimit,
    showTagList,
    collapseTagList,
    handleDragged,
    handlePaste,
    handleInput,
    handleKeydown,
    handleKeyup,
    handleAddTag,
    handleRemoveTag,
    handleClear,
    handleCompositionStart,
    handleCompositionUpdate,
    handleCompositionEnd,
    focus,
    blur
  };
}
exports.useInputTag = useInputTag;
