/**
 * ldesign v0.3.13
 * (c) 2023 ldesign
 * @license MIT
 */

import { _ as _defineProperty } from '../_chunks/dep-2db7b3ec.mjs';
import { _ as _toConsumableArray } from '../_chunks/dep-bf1e33c8.mjs';
import { _ as _slicedToArray } from '../_chunks/dep-84acf3c2.mjs';
import { toRefs, ref, createVNode, mergeProps } from 'vue';
import { Tag } from '../tag/index.mjs';
import useVModel from '../hooks/useVModel.mjs';
import { usePrefixClass } from '../hooks/useConfig.mjs';
import { useTNodeJSX } from '../hooks/tnode.mjs';
import '../_chunks/dep-255303ba.mjs';
import '../_chunks/dep-b8d7992c.mjs';
import '../utils/withInstall.mjs';
import '../tag/tag.mjs';
import '@ldesign/icons-vue-next';
import '../_chunks/dep-876fb3ec.mjs';
import '../_chunks/dep-eba0ea6e.mjs';
import '../_chunks/dep-469b4ae0.mjs';
import '../_chunks/dep-b57180d0.mjs';
import '../_chunks/dep-c8d7b6b4.mjs';
import '../_chunks/dep-3da99438.mjs';
import '../hooks/useGlobalIcon.mjs';
import '../config-provider/useConfig.mjs';
import '../_chunks/dep-095107f7.mjs';
import '../_chunks/dep-c989dff3.mjs';
import '../_chunks/dep-9cf86cf1.mjs';
import '../_chunks/dep-bc1d737a.mjs';
import '../_chunks/dep-4d7a675b.mjs';
import '../_chunks/dep-5717e263.mjs';
import '../_chunks/dep-a8bcacea.mjs';
import '../_chunks/dep-2e7f59b1.mjs';
import '../_chunks/dep-855db8bc.mjs';
import '../_chunks/dep-a87f3e4c.mjs';
import '../_chunks/dep-1e566bb5.mjs';
import '../_chunks/dep-1434aa15.mjs';
import '../_chunks/dep-e5eb9f23.mjs';
import '../_chunks/dep-9fe2ef12.mjs';
import '../_chunks/dep-672e7d1a.mjs';
import '../_chunks/dep-42a19209.mjs';
import '../_chunks/dep-9262ee2f.mjs';
import '../_chunks/dep-334a4f6b.mjs';
import '../_chunks/dep-8d3e9aa3.mjs';
import '../_chunks/dep-40d69a9c.mjs';
import '../_chunks/dep-dcc83161.mjs';
import '../_chunks/dep-0fc02184.mjs';
import '../_chunks/dep-f1994936.mjs';
import '../_chunks/dep-345e1be3.mjs';
import '../_common/js/global-config/default-config.mjs';
import '../_common/js/global-config/locale/zh_CN.mjs';
import '../_chunks/dep-9888611c.mjs';
import '../_chunks/dep-697a1365.mjs';
import '../config-provider/type.mjs';
import '../tag/props.mjs';
import '../_chunks/dep-406cd044.mjs';
import '../_chunks/dep-42cc3015.mjs';
import '../_chunks/dep-2bc37d52.mjs';
import '../_chunks/dep-327559e8.mjs';
import '../_chunks/dep-b2235f26.mjs';
import '../_chunks/dep-8b989474.mjs';
import '../_chunks/dep-1372b2d3.mjs';
import '../_chunks/dep-9a85dac1.mjs';
import '../utils/render-tnode.mjs';
import '../_chunks/dep-888e80c4.mjs';
import '../tag/check-tag.mjs';
import '../tag/check-tag-props.mjs';
import '../tag/style/css.mjs';
import '../tag/type.mjs';

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function useTagList(props) {
  var renderTNode = useTNodeJSX();
  var classPrefix = usePrefixClass();
  var _toRefs = toRefs(props),
    value = _toRefs.value,
    modelValue = _toRefs.modelValue,
    onRemove = _toRefs.onRemove,
    max = _toRefs.max,
    minCollapsedNum = _toRefs.minCollapsedNum,
    size = _toRefs.size,
    disabled = _toRefs.disabled,
    readonly = _toRefs.readonly,
    tagProps = _toRefs.tagProps,
    getDragProps = _toRefs.getDragProps;
  var _useVModel = useVModel(value, modelValue, props.defaultValue || [], props.onChange),
    _useVModel2 = _slicedToArray(_useVModel, 2),
    tagValue = _useVModel2[0],
    setTagValue = _useVModel2[1];
  var oldInputValue = ref();
  var _onClose = function onClose(p) {
    var _onRemove$value;
    var arr = _toConsumableArray(tagValue.value);
    arr.splice(p.index, 1);
    setTagValue(arr, _objectSpread({
      trigger: "tag-remove"
    }, p));
    (_onRemove$value = onRemove.value) === null || _onRemove$value === void 0 ? void 0 : _onRemove$value.call(onRemove, _objectSpread(_objectSpread({}, p), {}, {
      trigger: "tag-remove",
      value: arr
    }));
  };
  var clearAll = function clearAll(context) {
    setTagValue([], {
      trigger: "clear",
      e: context.e
    });
  };
  var onInnerEnter = function onInnerEnter(value2, context) {
    var _tagValue$value, _props$onEnter;
    var valueStr = value2 ? String(value2).trim() : "";
    var isLimitExceeded = max.value && ((_tagValue$value = tagValue.value) === null || _tagValue$value === void 0 ? void 0 : _tagValue$value.length) >= max.value;
    var newValue = tagValue.value;
    if (!isLimitExceeded && valueStr) {
      newValue = Array.isArray(tagValue.value) ? tagValue.value.concat(String(valueStr)) : [valueStr];
      setTagValue(newValue, {
        trigger: "enter",
        index: newValue.length - 1,
        item: valueStr,
        e: context.e
      });
    }
    props === null || props === void 0 ? void 0 : (_props$onEnter = props.onEnter) === null || _props$onEnter === void 0 ? void 0 : _props$onEnter.call(props, newValue, _objectSpread(_objectSpread({}, context), {}, {
      inputValue: value2
    }));
  };
  var onInputBackspaceKeyUp = function onInputBackspaceKeyUp(value2, context) {
    var e = context.e;
    if (!tagValue.value || !tagValue.value.length || e.key === "Process") return;
    var isDelete = /(Backspace|NumpadDelete)/i.test(e.code) || /(Backspace|NumpadDelete)/i.test(e.key);
    if (!oldInputValue.value && isDelete) {
      var _onRemove$value2;
      var index = tagValue.value.length - 1;
      var item = tagValue.value[index];
      var trigger = "backspace";
      var newValue = tagValue.value.slice(0, -1);
      setTagValue(newValue, {
        e: e,
        index: index,
        item: item,
        trigger: trigger
      });
      (_onRemove$value2 = onRemove.value) === null || _onRemove$value2 === void 0 ? void 0 : _onRemove$value2.call(onRemove, {
        e: e,
        index: index,
        item: item,
        trigger: trigger,
        value: newValue
      });
    }
    oldInputValue.value = value2;
  };
  var renderLabel = function renderLabel(_ref) {
    var displayNode = _ref.displayNode,
      label = _ref.label;
    var newList = minCollapsedNum.value ? tagValue.value.slice(0, minCollapsedNum.value) : tagValue.value;
    var list = displayNode ? [displayNode] : newList === null || newList === void 0 ? void 0 : newList.map(function (item, index) {
      var _getDragProps$value;
      var tagContent = renderTNode("tag", {
        params: {
          value: item
        }
      });
      return createVNode(Tag, mergeProps({
        "key": "".concat(item).concat(index),
        "size": size.value,
        "disabled": disabled.value,
        "onClose": function onClose(context) {
          return _onClose({
            e: context.e,
            item: item,
            index: index
          });
        },
        "closable": !readonly.value && !disabled.value
      }, (_getDragProps$value = getDragProps.value) === null || _getDragProps$value === void 0 ? void 0 : _getDragProps$value.call(getDragProps, index, item), tagProps.value), {
        "default": function _default() {
          return [tagContent !== null && tagContent !== void 0 ? tagContent : item];
        }
      });
    });
    if (![null, void 0, ""].includes(label)) {
      list.unshift(createVNode("div", {
        "class": "".concat(classPrefix.value, "-tag-input__prefix"),
        "key": "label"
      }, [label]));
    }
    if (newList.length !== (tagValue.value || []).length) {
      var len = tagValue.value.length - newList.length;
      var more = renderTNode("collapsedItems", {
        params: {
          value: tagValue.value,
          count: tagValue.value.length - minCollapsedNum.value,
          collapsedTags: tagValue.value.slice(minCollapsedNum.value, tagValue.value.length),
          collapsedSelectedItems: tagValue.value.slice(minCollapsedNum.value, tagValue.value.length)
        }
      });
      list.push(more !== null && more !== void 0 ? more : createVNode(Tag, {
        "key": "more"
      }, {
        "default": function _default() {
          return ["+", len];
        }
      }));
    }
    return list;
  };
  return {
    tagValue: tagValue,
    clearAll: clearAll,
    onClose: _onClose,
    onInnerEnter: onInnerEnter,
    onInputBackspaceKeyUp: onInputBackspaceKeyUp,
    renderLabel: renderLabel
  };
}

export { useTagList as default };
//# sourceMappingURL=useTagList.mjs.map
