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

import { h as helper } from '../../../_chunks/dep-b0f1ed22.js';
import { toRefs, ref } from '@vue/composition-api';
import { Tag } from '../../tag/index.js';
import useVModel from '../../../hooks/useVModel.js';
import { useLNodeJSX } from '../../../hooks/lnode.js';
import { useConfig } from '../../config-provider/useConfig.js';
import '../../../utils/withInstall.js';
import 'vue';
import 'lodash/capitalize';
import '../../../config.js';
import '../../../utils/map-props.js';
import '../../tag/tag.js';
import '@ldesign/icons-vue';
import '../../../utils/mixins.js';
import '../../config-provider/config-receiver.js';
import 'lodash/mergeWith';
import '../../config-provider/context.js';
import 'lodash/merge';
import '../../../_chunks/dep-b4fd1955.js';
import '../../../utils/render-node.js';
import 'lodash';
import '../../../utils/event.js';
import '../../../utils/helper.js';
import 'lodash/camelCase';
import '../../tag/props.js';
import '../../tag/check-tag.js';
import 'lodash/kebabCase';
import 'lodash/isEmpty';
import 'lodash/isFunction';
import '../../../hooks/render-lnode.js';

function useTagList(props, getDragProps) {
  const renderTNode = useLNodeJSX();
  const {
    classPrefix
  } = useConfig("classPrefix");
  const {
    value,
    onRemove,
    max,
    minCollapsedNum,
    size,
    disabled,
    readonly,
    tagProps
  } = toRefs(props);
  const [tagValue, setTagValue] = useVModel(value, props.defaultValue || [], props.onChange, "change");
  const oldInputValue = 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(Tag, 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(Tag, {
        "key": "more"
      }, ["+", len]));
    }
    return list;
  };
  return {
    tagValue,
    clearAll,
    onClose,
    onInnerEnter,
    onInputBackspaceKeyUp,
    renderLabel
  };
}

export { useTagList as default };
//# sourceMappingURL=use-tag-list.js.map
