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

'use strict';

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

var throttle = require('lodash/throttle');
var VueCompositionAPI = require('@vue/composition-api');

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

var throttle__default = /*#__PURE__*/_interopDefaultLegacy(throttle);

function useDraggable(props, root) {
  const dragStates = VueCompositionAPI.reactive({
    isDragOver: false,
    isDragging: false,
    dropPosition: 0
  });
  const updateDropPosition = dragEvent => {
    const rootNode = root.value;
    if (!rootNode) return;
    const rect = rootNode?.getBoundingClientRect?.();
    const offsetY = window.pageYOffset + rect.top;
    const {
      pageY
    } = dragEvent;
    const gapHeight = rect.height / 4;
    const diff = pageY - offsetY;
    if (diff < gapHeight) dragStates.dropPosition = -1;else if (diff < rect.height - gapHeight) dragStates.dropPosition = 0;else dragStates.dropPosition = 1;
  };
  const setDragStatus = (status, dragEvent) => {
    const {
      node,
      treeScope
    } = props;
    const {
      drag
    } = treeScope;
    if (!drag) return;
    switch (status) {
      case "dragStart":
        dragStates.isDragging = true;
        dragStates.dropPosition = 0;
        drag.handleDragStart?.({
          node,
          dragEvent
        });
        break;
      case "dragEnd":
        dragStates.isDragging = false;
        dragStates.isDragOver = false;
        dragStates.dropPosition = 0;
        throttleUpdateDropPosition.cancel();
        drag.handleDragEnd?.({
          node,
          dragEvent
        });
        break;
      case "dragOver":
        dragStates.isDragOver = true;
        throttleUpdateDropPosition(dragEvent);
        drag.handleDragOver?.({
          node,
          dragEvent
        });
        break;
      case "dragLeave":
        dragStates.isDragOver = false;
        dragStates.dropPosition = 0;
        throttleUpdateDropPosition.cancel();
        drag.handleDragLeave?.({
          node,
          dragEvent
        });
        break;
      case "drop":
        drag.handleDrop?.({
          node,
          dropPosition: dragStates.dropPosition,
          dragEvent
        });
        dragStates.isDragOver = false;
        throttleUpdateDropPosition.cancel();
        break;
    }
  };
  const handleDragStart = evt => {
    const {
      node
    } = props;
    if (!node.isDraggable()) return;
    evt.stopPropagation();
    setDragStatus("dragStart", evt);
    try {
      evt.dataTransfer?.setData("text/plain", "");
    } catch (e) {}
  };
  const handleDragEnd = evt => {
    const {
      node
    } = props;
    if (!node.isDraggable()) return;
    evt.stopPropagation();
    setDragStatus("dragEnd", evt);
  };
  const handleDragOver = evt => {
    const {
      node
    } = props;
    if (!node.isDraggable()) return;
    evt.stopPropagation();
    evt.preventDefault();
    setDragStatus("dragOver", evt);
  };
  const handleDragLeave = evt => {
    const {
      node
    } = props;
    if (!node.isDraggable()) return;
    evt.stopPropagation();
    setDragStatus("dragLeave", evt);
  };
  const handleDrop = evt => {
    const {
      node
    } = props;
    if (!node.isDraggable()) return;
    evt.stopPropagation();
    evt.preventDefault();
    setDragStatus("drop", evt);
  };
  const throttleUpdateDropPosition = throttle__default["default"](dragEvent => {
    updateDropPosition(dragEvent);
  });
  return {
    dragStates,
    handleDragStart,
    handleDragEnd,
    handleDragOver,
    handleDragLeave,
    handleDrop
  };
}

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