"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useDraggable = void 0;
var react_1 = require("react");
var utilities_1 = require("@dnd-kit/utilities");
var useDndContext_1 = require("./useDndContext");
var useDraggable = function (props) {
    var _a = (0, useDndContext_1.useDndContext)(), draggableNodesRef = _a.draggableNodesRef, onDragStart = _a.onDragStart, onDragEnd = _a.onDragEnd;
    var onChangeNode = (0, react_1.useCallback)(function (newElement) {
        if (newElement) {
            newElement.setAttribute('data-test-dnd-drag-id', props.id);
        }
    }, [props.id]);
    var _b = (0, utilities_1.useNodeRef)(onChangeNode), node = _b[0], setNodeRef = _b[1];
    var onChangeActivatorNode = (0, react_1.useCallback)(function (newElement) {
        if (newElement) {
            newElement.setAttribute('data-test-dnd-activator-id', props.id);
        }
    }, [props.id]);
    var _c = (0, utilities_1.useNodeRef)(onChangeActivatorNode), setActivatorNodeRef = _c[1];
    var data = (0, react_1.useRef)(props.data);
    (0, react_1.useEffect)(function () {
        draggableNodesRef.current.set(props.id, { id: props.id, data: data, node: node });
        return function () {
            var node = draggableNodesRef.current.get(props.id);
            if (node) {
                draggableNodesRef.current.delete(props.id);
            }
        };
    }, [props.id]);
    var listeners = (0, react_1.useMemo)(function () {
        return props.disabled
            ? {}
            : {
                onMouseDown: function () {
                    onDragStart(props.id);
                },
                onMouseUp: function () {
                    onDragEnd();
                }
            };
    }, [props.id, onDragStart, onDragEnd, props.disabled]);
    return {
        attributes: {},
        listeners: listeners,
        setNodeRef: setNodeRef,
        setActivatorNodeRef: setActivatorNodeRef
    };
};
exports.useDraggable = useDraggable;
