/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

import { h as helper } from '../../../_chunks/dep-29915b8e.js';
import { toRefs, ref } from '@vue/composition-api';
import { Tag } from '../../tag/index.js';
import useVModel from '../../../hooks/use-vmodel.js';
import { useLNodeJSX } from '../../../hooks/lnode.js';
import { useConfig } from '../../config-provider/useConfig.js';
import '../../../utils/with-install.js';
import 'vue';
import 'lodash';
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 '../../../_chunks/dep-def4c2ce.js';
import '../../../utils/render-node.js';
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 => {
    var _onRemove$value;
    const arr = [...tagValue.value];
    arr.splice(p.index, 1);
    setTagValue(arr, {
      trigger: "tag-remove",
      ...p
    });
    (_onRemove$value = onRemove.value) === null || _onRemove$value === void 0 ? void 0 : _onRemove$value.call(onRemove, {
      ...p,
      trigger: "tag-remove",
      value: arr
    });
  };
  const clearAll = context => {
    setTagValue([], {
      trigger: "clear",
      e: context.e
    });
  };
  const onInnerEnter = (value2, context) => {
    var _tagValue$value, _props$onEnter;
    const valueStr = String(value2).trim();
    if (!valueStr) return;
    const isLimitExceeded = max.value && ((_tagValue$value = tagValue.value) === null || _tagValue$value === void 0 ? void 0 : _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 === null || props === void 0 ? void 0 : (_props$onEnter = props.onEnter) === null || _props$onEnter === void 0 ? void 0 : _props$onEnter.call(props, 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)) {
      var _onRemove$value2;
      const index = tagValue.value.length - 1;
      const item = tagValue.value[index];
      const trigger = "backspace";
      setTagValue(tagValue.value.slice(0, -1), {
        e,
        index,
        item,
        trigger
      });
      (_onRemove$value2 = onRemove.value) === null || _onRemove$value2 === void 0 ? void 0 : _onRemove$value2.call(onRemove, {
        e,
        index,
        item,
        trigger,
        value: tagValue.value
      });
    }
    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 === null || newList === void 0 ? void 0 : 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 === null || getDragProps === void 0 ? void 0 : 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
