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

'use strict';

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

var helper = require('../_chunks/dep-99705d51.js');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _toConsumableArray = require('@babel/runtime/helpers/toConsumableArray');
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var VueCompositionAPI = require('@vue/composition-api');
var tag_index = require('../tag/index.js');
var config = require('../config.js');
var hooks_useVModel = require('../hooks/useVModel.js');
var hooks_tnode = require('../hooks/tnode.js');
require('../tag/tag.js');
require('@babel/runtime/helpers/typeof');
require('tdesign-icons-vue');
require('../utils/classnames.js');
require('../tag/props.js');
require('../utils/mixins.js');
require('vue');
require('../config-provider/config-receiver.js');
require('lodash/mergeWith');
require('../config-provider/context.js');
require('lodash/merge');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');
require('../utils/render-tnode.js');
require('../utils/event.js');
require('../utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('lodash/camelCase');
require('../tag/check-tag.js');
require('../tag/check-tag-props.js');
require('../utils/withInstall.js');
require('lodash/capitalize');
require('../utils/map-props.js');
require('lodash/isEmpty');
require('lodash/isFunction');
require('lodash/kebabCase');
require('../hooks/render-tnode.js');
require('lodash/isString');
require('lodash/isObject');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var _toConsumableArray__default = /*#__PURE__*/_interopDefaultLegacy(_toConsumableArray);
var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);

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__default["default"](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 = hooks_tnode.useTNodeJSX();

  var _toRefs = VueCompositionAPI.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 = hooks_useVModel["default"](value, props.defaultValue || [], props.onChange, "change"),
      _useVModel2 = _slicedToArray__default["default"](_useVModel, 2),
      tagValue = _useVModel2[0],
      setTagValue = _useVModel2[1];

  var oldInputValue = VueCompositionAPI.ref();

  var onClose = function onClose(p) {
    var _onRemove$value;

    var arr = _toConsumableArray__default["default"](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
        }
      });
      return h(tag_index.Tag, helper.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]);
    });

    if (![null, void 0, ""].includes(label)) {
      list.unshift(h("div", {
        "class": "".concat(config.prefix, "-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,
          count: tagValue.value.length,
          collapsedTags: tagValue.value.slice(minCollapsedNum.value, tagValue.value.length)
        }
      });
      list.push(more !== null && more !== void 0 ? more : h(tag_index.Tag, {
        "key": "more"
      }, ["+", len]));
    }

    return list;
  };

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

exports["default"] = useTagList;
//# sourceMappingURL=useTagList.js.map
