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

'use strict';

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

var components_tree_utils = require('../utils.js');
require('lodash/camelCase');

function useDragHandle(props, context, treeState) {
  const {
    store
  } = treeState;
  let dragNode = null;
  const handleDragStart = state => {
    const {
      dragEvent,
      node
    } = state;
    dragNode = node;
    const ctx = {
      node: node.getModel(),
      e: dragEvent
    };
    components_tree_utils.emitEvent(props, context, "drag-start", ctx);
  };
  const handleDragEnd = state => {
    const {
      dragEvent,
      node
    } = state;
    dragNode = node;
    const ctx = {
      node: node.getModel(),
      e: dragEvent
    };
    components_tree_utils.emitEvent(props, context, "drag-end", ctx);
  };
  const handleDragOver = state => {
    const {
      dragEvent,
      node
    } = state;
    const ctx = {
      node: node.getModel(),
      e: dragEvent
    };
    components_tree_utils.emitEvent(props, context, "drag-over", ctx);
  };
  const handleDragLeave = state => {
    const {
      dragEvent,
      node
    } = state;
    const ctx = {
      node: node.getModel(),
      e: dragEvent
    };
    components_tree_utils.emitEvent(props, context, "drag-leave", ctx);
  };
  const handleDrop = state => {
    const {
      dragEvent,
      node,
      dropPosition
    } = state;
    if (node.value === dragNode.value || node.getParents().some(_node => _node.value === dragNode.value)) return;
    const nodes = store.getNodes();
    nodes.some(_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;
    });
    const ctx = {
      dropNode: node.getModel(),
      dragNode: dragNode.getModel(),
      dropPosition,
      e: dragEvent
    };
    components_tree_utils.emitEvent(props, context, "drop", ctx);
  };
  const drag = {
    handleDragStart,
    handleDragEnd,
    handleDragOver,
    handleDragLeave,
    handleDrop
  };
  const {
    cache
  } = treeState;
  const {
    scope
  } = cache;
  scope.drag = drag;
  return {
    drag
  };
}

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