/**
 * ldesign v0.2.22
 * (c) 2022/12/7 16:21 built by swimly
 * @license MIT
 */

'use strict';

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

var helper = require('../../../chunks/helper.js');
var VueCompositionAPI = require('@vue/composition-api');
var components_tag_index = require('../../tag/index.js');
var hooks_useVModel = require('../../../hooks/useVModel.js');
var hooks_lnode = require('../../../hooks/lnode.js');
var components_configProvider_useConfig = require('../../config-provider/useConfig.js');
require('../../../utils/withInstall.js');
require('vue');
require('lodash/capitalize');
require('../../../config.js');
require('../../../utils/map-props.js');
require('../../tag/tag.js');
require('@ldesign/icons-vue');
require('../../../utils/mixins.js');
require('../../config-provider/config-receiver.js');
require('lodash/mergeWith');
require('../../config-provider/context.js');
require('lodash/merge');
require('../../../chunks/zh_CN.js');
require('../../../utils/render-node.js');
require('lodash');
require('../../../utils/event.js');
require('../../../utils/helper.js');
require('lodash/camelCase');
require('../../tag/props.js');
require('../../tag/check-tag.js');
require('lodash/kebabCase');
require('lodash/isEmpty');
require('lodash/isFunction');
require('../../../hooks/render-lnode.js');

function useTagList(props, getDragProps) {
  const renderTNode = hooks_lnode.useLNodeJSX();
  const {
    classPrefix
  } = components_configProvider_useConfig.useConfig("classPrefix");
  const {
    value,
    onRemove,
    max,
    minCollapsedNum,
    size,
    disabled,
    readonly,
    tagProps
  } = VueCompositionAPI.toRefs(props);
  const [tagValue, setTagValue] = hooks_useVModel["default"](value, props.defaultValue || [], props.onChange, "change");
  const oldInputValue = VueCompositionAPI.ref();
  const onClose = p => {
    const arr = [...tagValue.value];
    arr.splice(p.index, 1);
    setTagValue(arr, {
      trigger: "tag-remove",
      ...p
    });
    onRemove.value?.({
      ...p,
      trigger: "tag-remove",
      value: arr
    });
  };
  const clearAll = context => {
    setTagValue([], {
      trigger: "clear",
      e: context.e
    });
  };
  const onInnerEnter = (value2, context) => {
    const valueStr = String(value2).trim();
    if (!valueStr) return;
    const isLimitExceeded = max.value && tagValue.value?.length >= max.value;
    let newValue = tagValue.value;
    if (!isLimitExceeded) {
      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?.onEnter?.(newValue, {
      ...context,
      inputValue: value2
    });
  };
  const onInputBackspaceKeyUp = (value2, context) => {
    const {
      e
    } = context;
    if (!tagValue.value || !tagValue.value.length) return;
    if (!oldInputValue.value && ["Backspace", "NumpadDelete"].includes(e.code)) {
      const index = tagValue.value.length - 1;
      const item = tagValue.value[index];
      const trigger = "backspace";
      const newValue = tagValue.value.slice(0, -1);
      const params = {
        e,
        index,
        item,
        trigger
      };
      setTagValue(newValue, params);
      onRemove.value?.({
        ...params,
        value: newValue
      });
    }
    oldInputValue.value = value2;
  };
  const renderLabel = (_ref, h) => {
    let {
      displayNode,
      label
    } = _ref;
    const newList = minCollapsedNum.value ? tagValue.value.slice(0, minCollapsedNum.value) : tagValue.value;
    const list = displayNode ? [displayNode] : newList?.map((item, index) => {
      const tagContent = renderTNode("tag", {
        params: {
          value: item
        }
      });
      const TagNode = h(components_tag_index.Tag, helper.helper([{
        "key": index,
        "attrs": {
          "size": size.value,
          "disabled": disabled.value,
          "closable": !readonly.value && !disabled.value
        },
        "on": {
          "close": context => onClose({
            e: context.e,
            item,
            index
          })
        }
      }, {
        "props": tagProps.value
      }]), [tagContent ?? item]);
      const itemDrag = getDragProps?.(index, item);
      return itemDrag && itemDrag.draggable ? h("span", {
        "class": `${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": `${classPrefix.value}-tag-input__prefix`,
        "key": "label"
      }, [label]));
    }
    if (newList.length !== tagValue.value.length) {
      const len = tagValue.value.length - newList.length;
      const 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 ?? h(components_tag_index.Tag, {
        "key": "more"
      }, ["+", len]));
    }
    return list;
  };
  return {
    tagValue,
    clearAll,
    onClose,
    onInnerEnter,
    onInputBackspaceKeyUp,
    renderLabel
  };
}

exports["default"] = useTagList;
//# sourceMappingURL=use-tag-list.js.map
