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

import { ref, reactive } from '@vue/composition-api';

function useDragSorter(props, context) {
  const {
    sortOnDraggable,
    onDragSort,
    onDragOverCheck
  } = props;
  const draggingIndex = ref(-1);
  const dragStartData = ref(null);
  const isDropped = ref(null);
  const startInfo = reactive({
    nodeX: 0,
    nodeWidth: 0,
    mouseX: 0
  });
  const onDragOver = (e, index, record) => {
    e.preventDefault();
    if (draggingIndex.value === index || draggingIndex.value === -1) return;
    const target = e.target;
    if (onDragOverCheck !== null && onDragOverCheck !== void 0 && onDragOverCheck.targetClassNameRegExp && !(onDragOverCheck !== null && onDragOverCheck !== void 0 && onDragOverCheck.targetClassNameRegExp.test(target.className))) return;
    if (onDragOverCheck !== null && onDragOverCheck !== void 0 && onDragOverCheck.x && target) {
      if (!startInfo.nodeWidth) return;
      const {
        x,
        width
      } = target.getBoundingClientRect();
      const targetNodeMiddleX = x + width / 2;
      const draggingNodeLeft = e.clientX - (startInfo.mouseX - startInfo.nodeX);
      const draggingNodeRight = draggingNodeLeft + startInfo.nodeWidth;
      let overlap = false;
      if (draggingNodeLeft > x && draggingNodeLeft < x + width) overlap = draggingNodeLeft < targetNodeMiddleX;else overlap = draggingNodeRight > targetNodeMiddleX;
      if (!overlap) return;
    }
    const data = {
      currentIndex: draggingIndex.value,
      current: dragStartData,
      target: record,
      targetIndex: index
    };
    onDragSort === null || onDragSort === void 0 ? void 0 : onDragSort(data);
    context.emit("drag-sort", data);
    draggingIndex.value = index;
  };
  if (!sortOnDraggable) return {};
  function onDragStart(e, index, record) {
    draggingIndex.value = index;
    dragStartData.value = record;
    const target = e.target;
    if (onDragOverCheck && target) {
      const {
        x,
        width
      } = target.getBoundingClientRect();
      startInfo.nodeX = x;
      startInfo.nodeWidth = width;
      startInfo.mouseX = e.clientX;
    }
  }
  function onDrop() {
    isDropped.value = true;
  }
  function onDragEnd() {
    if (!isDropped.value) {}
    isDropped.value = false;
    draggingIndex.value = -1;
    dragStartData.value = null;
  }
  function getDragProps(index, record) {
    if (sortOnDraggable) {
      return {
        draggable: true,
        onDragstart: e => {
          onDragStart(e, index, record);
        },
        onDragover: e => {
          onDragOver(e, index, record);
        },
        onDrop: () => {
          onDrop();
        },
        onDragend: () => {
          onDragEnd();
        }
      };
    }
  }
  return {
    onDragStart,
    onDragOver,
    onDrop,
    onDragEnd,
    getDragProps,
    dragging: draggingIndex.value !== -1
  };
}

export { useDragSorter as default };
//# sourceMappingURL=use-drag-sorter.js.map
