"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = (exports1, definition)=>{
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
})();
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    useStageTaskDragHandler: ()=>useStageTaskDragHandler
});
const react_namespaceObject = require("@xyflow/react");
const external_react_namespaceObject = require("react");
const external_StageNode_utils_cjs_namespaceObject = require("../StageNode.utils.cjs");
const useStageTaskDragHandler = ({ sequentialTaskGroups, sequentialTasks, onTaskReorder })=>{
    const storeApi = (0, react_namespaceObject.useStoreApi)();
    const [activeDragId, setActiveDragId] = (0, external_react_namespaceObject.useState)(null);
    const [offsetLeft, setOffsetLeft] = (0, external_react_namespaceObject.useState)(0);
    const [overId, setOverId] = (0, external_react_namespaceObject.useState)(null);
    const activeSequentialTask = (0, external_react_namespaceObject.useMemo)(()=>sequentialTasks.find(({ task })=>task.id === activeDragId), [
        sequentialTasks,
        activeDragId
    ]);
    const isActiveTaskParallel = (0, external_react_namespaceObject.useMemo)(()=>{
        if (!activeDragId) return false;
        const group = sequentialTaskGroups.find((g)=>g.some((t)=>t.id === activeDragId));
        return group ? group.length > 1 : false;
    }, [
        sequentialTaskGroups,
        activeDragId
    ]);
    const projected = (0, external_react_namespaceObject.useMemo)(()=>{
        if (!activeDragId || !overId) return null;
        return (0, external_StageNode_utils_cjs_namespaceObject.getProjection)(sequentialTaskGroups, activeDragId, overId, offsetLeft);
    }, [
        sequentialTaskGroups,
        activeDragId,
        overId,
        offsetLeft
    ]);
    const resetState = (0, external_react_namespaceObject.useCallback)(()=>{
        setActiveDragId(null);
        setOffsetLeft(0);
        setOverId(null);
    }, []);
    const handleDragStart = (0, external_react_namespaceObject.useCallback)((event)=>{
        setActiveDragId(event.active.id);
    }, []);
    const handleDragMove = (0, external_react_namespaceObject.useCallback)((event)=>{
        setOffsetLeft(event.delta.x / storeApi.getState().transform[2]);
    }, [
        storeApi
    ]);
    const handleDragOver = (0, external_react_namespaceObject.useCallback)((event)=>{
        setOverId(event.over?.id ?? null);
    }, []);
    const handleDragEnd = (0, external_react_namespaceObject.useCallback)((event)=>{
        const { active, over } = event;
        const currentOffsetLeft = offsetLeft;
        resetState();
        if (!over) return;
        const projection = (0, external_StageNode_utils_cjs_namespaceObject.getProjection)(sequentialTaskGroups, active.id, over.id, currentOffsetLeft);
        if (!projection) return;
        if (active.id === over.id) {
            const flattened = (0, external_StageNode_utils_cjs_namespaceObject.flattenTasks)(sequentialTaskGroups);
            const activeTask = flattened.find((t)=>t.id === active.id);
            if (activeTask && activeTask.depth === projection.depth) return;
        }
        const newTasks = (0, external_StageNode_utils_cjs_namespaceObject.reorderTasks)(sequentialTaskGroups, active.id, over.id, projection.depth);
        onTaskReorder(newTasks);
    }, [
        sequentialTaskGroups,
        onTaskReorder,
        offsetLeft,
        resetState
    ]);
    const handleDragCancel = (0, external_react_namespaceObject.useCallback)(()=>{
        resetState();
    }, [
        resetState
    ]);
    return {
        activeDragId,
        activeSequentialTask,
        isActiveTaskParallel,
        projected,
        handleDragMove,
        handleDragOver,
        handleDragStart,
        handleDragEnd,
        handleDragCancel
    };
};
exports.useStageTaskDragHandler = __webpack_exports__.useStageTaskDragHandler;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "useStageTaskDragHandler"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
