/**
 * tdesign v0.46.4
 * (c) 2022 tdesign
 * @license MIT
 */

import { h as helper } from '../_chunks/dep-a4bd011a.js';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import { toRefs, ref } from '@vue/composition-api';
import { Tag } from '../tag/index.js';
import useVModel from '../hooks/useVModel.js';
import { useTNodeJSX } from '../hooks/tnode.js';
import { useConfig } from '../config-provider/useConfig.js';
import '../tag/tag.js';
import '@babel/runtime/helpers/typeof';
import 'tdesign-icons-vue';
import '../tag/props.js';
import '../utils/mixins.js';
import 'vue';
import '../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import '../config-provider/context.js';
import 'lodash/merge';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '../config-provider/type.js';
import '../utils/render-tnode.js';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import '../utils/event.js';
import '../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '../tag/check-tag.js';
import '../tag/check-tag-props.js';
import '../utils/withInstall.js';
import 'lodash/capitalize';
import '../utils/map-props.js';
import './style/index.js';
import '../tag/type.js';
import 'lodash/isEmpty';
import 'lodash/isFunction';
import '../hooks/render-tnode.js';
import 'lodash/isString';
import 'lodash/isObject';

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, getDragProps) {
  var renderTNode = useTNodeJSX();

  var _useConfig = useConfig("classPrefix"),
      classPrefix = _useConfig.classPrefix;

  var _toRefs = toRefs(props),
      value = _toRefs.value,
      onRemove = _toRefs.onRemove,
      max = _toRefs.max,
      minCollapsedNum = _toRefs.minCollapsedNum,
      size = _toRefs.size,
      disabled = _toRefs.disabled,
      readonly = _toRefs.readonly,
      tagProps = _toRefs.tagProps;

  var _useVModel = useVModel(value, props.defaultValue || [], props.onChange, "change"),
      _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 = String(value2).trim();
    if (!valueStr) return;
    var isLimitExceeded = max && ((_tagValue$value = tagValue.value) === null || _tagValue$value === void 0 ? void 0 : _tagValue$value.length) >= max.value;
    var newValue = tagValue.value;

    if (!isLimitExceeded) {
      newValue = tagValue.value instanceof Array ? 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) return;

    if (!oldInputValue.value && ["Backspace", "NumpadDelete"].includes(e.code)) {
      var _onRemove$value2;

      var index = tagValue.value.length - 1;
      var item = tagValue.value[index];
      var trigger = "backspace";
      setTagValue(tagValue.value.slice(0, -1), {
        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: tagValue.value
      });
    }

    oldInputValue.value = value2;
  };

  var renderLabel = function renderLabel(_ref, h) {
    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 tagContent = renderTNode("tag", {
        params: {
          value: item
        }
      });
      var TagNode = h(Tag, helper([{
        "key": index,
        "attrs": {
          "size": size.value,
          "disabled": disabled.value,
          "closable": !readonly.value && !disabled.value
        },
        "on": {
          "close": function close(context) {
            return onClose({
              e: context.e,
              item: item,
              index: index
            });
          }
        }
      }, {
        "props": tagProps.value
      }]), [tagContent !== null && tagContent !== void 0 ? tagContent : item]);
      var itemDrag = getDragProps === null || getDragProps === void 0 ? void 0 : getDragProps(index, item);
      return itemDrag && itemDrag.draggable ? h("span", {
        "class": "".concat(classPrefix.value, "-tag-input__drag_wrapper"),
        "attrs": {
          "draggable": true
        },
        "on": {
          "dragstart": itemDrag.onDragstart,
          "dragover": itemDrag.onDragover,
          "dragend": itemDrag.onDragend,
          "drop": itemDrag.onDrop
        }
      }, [TagNode]) : TagNode;
    });

    if (![null, void 0, ""].includes(label)) {
      list.unshift(h("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,
          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 : h(Tag, {
        "key": "more"
      }, ["+", len]));
    }

    return list;
  };

  return {
    tagValue: tagValue,
    clearAll: clearAll,
    onClose: onClose,
    onInnerEnter: onInnerEnter,
    onInputBackspaceKeyUp: onInputBackspaceKeyUp,
    renderLabel: renderLabel
  };
}

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