"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DndContextComponent = void 0;
var react_1 = __importStar(require("react"));
var DndContext_1 = require("./DndContext");
var DndMonitorContext_1 = require("./DndMonitorContext");
var useDndMonitorProvider_1 = require("./useDndMonitorProvider");
var DndContextComponent = function (_a) {
    var onDragStart = _a.onDragStart, onDragEnd = _a.onDragEnd, onDragOver = _a.onDragOver, children = _a.children;
    var draggableNodesRef = (0, react_1.useRef)(new Map());
    var droppableNodesRef = (0, react_1.useRef)(new Map());
    var _b = (0, useDndMonitorProvider_1.useDndMonitorProvider)(), dispatchMonitorEvent = _b[0], registerMonitorListener = _b[1];
    var dragActiveIdRef = (0, react_1.useRef)(null);
    var dragOverIdRef = (0, react_1.useRef)(null);
    var _c = (0, react_1.useState)(null), dragActive = _c[0], setDragActive = _c[1];
    var handleDragStart = (0, react_1.useCallback)(function (nodeId) {
        var draggableNode = draggableNodesRef.current.get(nodeId);
        dragActiveIdRef.current = nodeId;
        setDragActive(true);
        var event = {
            active: draggableNode
        };
        onDragStart === null || onDragStart === void 0 ? void 0 : onDragStart(event);
        dispatchMonitorEvent({
            type: 'onDragStart',
            event: event
        });
    }, [dispatchMonitorEvent, onDragStart]);
    var handleDragOver = (0, react_1.useCallback)(function (nodeId) {
        dragOverIdRef.current = nodeId;
        var draggableNode = draggableNodesRef.current.get(dragActiveIdRef.current);
        var droppableNode = droppableNodesRef.current.get(nodeId);
        var event = {
            active: draggableNode,
            over: droppableNode
        };
        onDragOver === null || onDragOver === void 0 ? void 0 : onDragOver(event);
        dispatchMonitorEvent({
            type: 'onDragOver',
            event: {
                active: draggableNode,
                over: droppableNode
            }
        });
    }, [dispatchMonitorEvent, onDragOver]);
    var handleDragEnd = (0, react_1.useCallback)(function () {
        var draggableNode = draggableNodesRef.current.get(dragActiveIdRef.current);
        var droppableNode = droppableNodesRef.current.get(dragOverIdRef.current);
        var event = {
            active: draggableNode,
            over: droppableNode
        };
        setDragActive(false);
        onDragEnd === null || onDragEnd === void 0 ? void 0 : onDragEnd(event);
        dispatchMonitorEvent({
            type: 'onDragEnd',
            event: event
        });
        dragActiveIdRef.current = null;
        dragOverIdRef.current = null;
    }, [dispatchMonitorEvent, onDragEnd]);
    var contextValue = (0, react_1.useMemo)(function () { return ({
        draggableNodesRef: draggableNodesRef,
        droppableNodesRef: droppableNodesRef,
        onDragStart: handleDragStart,
        onDragEnd: handleDragEnd,
        onDragOver: handleDragOver,
        dragActive: dragActive,
        measureDroppableContainers: function () { }
    }); }, [dragActive, handleDragStart, handleDragEnd, handleDragOver]);
    return (react_1.default.createElement(DndContext_1.DndContext.Provider, { value: contextValue },
        react_1.default.createElement(DndMonitorContext_1.DndMonitorContext.Provider, { value: registerMonitorListener }, children)));
};
exports.DndContextComponent = DndContextComponent;
