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

import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import { toRefs, ref, createVNode, mergeProps } from 'vue';
import { Tag } from '../tag/index.js';
import useVModel from '../hooks/useVModel.js';
import { usePrefixClass } from '../hooks/useConfig.js';
import { useTNodeJSX } from '../hooks/tnode.js';
import '../utils/withInstall.js';
import '../tag/tag.js';
import '@ldesign/icons-vue-next';
import 'lodash/isString';
import '../hooks/useGlobalIcon.js';
import '../config-provider/useConfig.js';
import 'lodash/cloneDeep';
import 'lodash/isFunction';
import '../config-provider/context.js';
import 'lodash/mergeWith';
import 'lodash/merge';
import 'lodash/isArray';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '@babel/runtime/helpers/typeof';
import '../_chunks/dep-e041f5b0.js';
import 'dayjs';
import '../tag/props.js';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../utils/render-tnode.js';
import 'lodash/isEmpty';
import 'lodash/isObject';
import '../tag/check-tag.js';
import '../tag/check-tag-props.js';

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.js.map
