/**
 * ldesign v0.4.6
 * (c) 2023 ldesign
 * @license MIT
 */

'use strict';

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

var tree_util = require('../util.js');
require('@babel/runtime/helpers/defineProperty');
require('lodash/camelCase');

function useDragHandle(props, context, state) {
  var treeState = state;
  var store = treeState.store;
  var dragNode = null;
  var handleDragStart = function handleDragStart(state2) {
    var dragEvent = state2.dragEvent,
      node = state2.node;
    dragNode = node;
    var ctx = {
      node: node.getModel(),
      e: dragEvent
    };
    tree_util.emitEvent(props, context, "drag-start", ctx);
  };
  var handleDragEnd = function handleDragEnd(state2) {
    var dragEvent = state2.dragEvent,
      node = state2.node;
    dragNode = node;
    var ctx = {
      node: node.getModel(),
      e: dragEvent
    };
    tree_util.emitEvent(props, context, "drag-end", ctx);
  };
  var handleDragOver = function handleDragOver(state2) {
    var dragEvent = state2.dragEvent,
      node = state2.node;
    var ctx = {
      node: node.getModel(),
      e: dragEvent
    };
    tree_util.emitEvent(props, context, "drag-over", ctx);
  };
  var handleDragLeave = function handleDragLeave(state2) {
    var dragEvent = state2.dragEvent,
      node = state2.node;
    var ctx = {
      node: node.getModel(),
      e: dragEvent
    };
    tree_util.emitEvent(props, context, "drag-leave", ctx);
  };
  var handleDrop = function handleDrop(state2) {
    var dragEvent = state2.dragEvent,
      node = state2.node,
      dropPosition = state2.dropPosition;
    if (node.value === dragNode.value || node.getParents().some(function (_node) {
      return _node.value === dragNode.value;
    })) return;
    var nodes = store.getNodes();
    nodes.some(function (_node) {
      if (_node.value === node.value) {
        if (dropPosition === 0) dragNode.appendTo(store, _node);else if (dropPosition < 0) node.insertBefore(dragNode);else node.insertAfter(dragNode);
        return true;
      }
      return false;
    });
    var ctx = {
      dropNode: node.getModel(),
      dragNode: dragNode.getModel(),
      dropPosition: dropPosition,
      e: dragEvent
    };
    tree_util.emitEvent(props, context, "drop", ctx);
  };
  var drag = {
    handleDragStart: handleDragStart,
    handleDragEnd: handleDragEnd,
    handleDragOver: handleDragOver,
    handleDragLeave: handleDragLeave,
    handleDrop: handleDrop
  };
  var scope = treeState.scope;
  scope.drag = drag;
  return {
    drag: drag
  };
}

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