"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__, {
    StageNodeSequentialTaskGroups: ()=>StageNodeSequentialTaskGroups
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const core_namespaceObject = require("@dnd-kit/core");
const sortable_namespaceObject = require("@dnd-kit/sortable");
const apollo_core_namespaceObject = require("@uipath/apollo-core");
const index_cjs_namespaceObject = require("../../layouts/index.cjs");
const external_react_namespaceObject = require("react");
const GroupModificationUtils_cjs_namespaceObject = require("../../utils/GroupModificationUtils.cjs");
const external_DraggableTask_cjs_namespaceObject = require("./DraggableTask.cjs");
const useStageTaskDragHandler_cjs_namespaceObject = require("./hooks/useStageTaskDragHandler.cjs");
const external_StageNode_styles_cjs_namespaceObject = require("./StageNode.styles.cjs");
const external_StageNodeTaskUtilities_cjs_namespaceObject = require("./StageNodeTaskUtilities.cjs");
const external_StageTaskDragOverlay_cjs_namespaceObject = require("./StageTaskDragOverlay.cjs");
const StageNodeSequentialTaskGroups = ({ props, sequentialTaskGroups, sequentialTasks, allTasks, isReadOnly, selectedTaskId, taskWidthStyle, hasContextMenu, handleTaskClick, handleReorderSequentialTasks, generateReplaceTaskMenuItemForTask })=>{
    const { execution, onTaskGroupModification, onTaskReorder, hideParallelOptions, loadingTaskIds } = props;
    const sequentialTaskIds = (0, external_react_namespaceObject.useMemo)(()=>sequentialTasks.map(({ task })=>task.id), [
        sequentialTasks
    ]);
    const { activeDragId, activeSequentialTask, isActiveTaskParallel, projected, handleDragStart, handleDragMove, handleDragOver, handleDragEnd, handleDragCancel } = (0, useStageTaskDragHandler_cjs_namespaceObject.useStageTaskDragHandler)({
        sequentialTaskGroups,
        sequentialTasks,
        onTaskReorder: handleReorderSequentialTasks
    });
    const handleTaskRegroup = (0, external_react_namespaceObject.useCallback)((groupModificationType, groupIndex, taskIndex)=>{
        if (onTaskReorder && (groupModificationType === GroupModificationUtils_cjs_namespaceObject.GroupModificationType.TASK_GROUP_UP || groupModificationType === GroupModificationUtils_cjs_namespaceObject.GroupModificationType.TASK_GROUP_DOWN)) {
            const mover = groupModificationType === GroupModificationUtils_cjs_namespaceObject.GroupModificationType.TASK_GROUP_UP ? GroupModificationUtils_cjs_namespaceObject.moveGroupUp : GroupModificationUtils_cjs_namespaceObject.moveGroupDown;
            const reordered = mover(sequentialTaskGroups, groupIndex, taskIndex);
            handleReorderSequentialTasks(reordered);
            return;
        }
        onTaskGroupModification?.(groupModificationType, groupIndex, taskIndex);
    }, [
        onTaskReorder,
        handleReorderSequentialTasks,
        onTaskGroupModification,
        sequentialTaskGroups
    ]);
    const buildContextMenuItems = (0, external_react_namespaceObject.useCallback)((groupIndex, _, taskId)=>{
        const taskGroup = sequentialTaskGroups[groupIndex] ?? [];
        const isParallel = taskGroup.length > 1;
        const items = [];
        const replaceTaskMenuItem = generateReplaceTaskMenuItemForTask(taskId, isParallel);
        if (replaceTaskMenuItem) {
            items.push(replaceTaskMenuItem);
            items.push((0, external_StageNodeTaskUtilities_cjs_namespaceObject.getDivider)());
        }
        let groupIndexInAllTasks;
        let taskIndexInAllTasks;
        for (const [allTasksGroupIndex, group] of allTasks.entries())for (const [allTasksTaskIndex, task] of group.entries())if (task.id === taskId) {
            groupIndexInAllTasks = allTasksGroupIndex;
            taskIndexInAllTasks = allTasksTaskIndex;
            break;
        }
        if (onTaskGroupModification && void 0 !== groupIndexInAllTasks && void 0 !== taskIndexInAllTasks) {
            const reGroupOptions = (0, external_StageNodeTaskUtilities_cjs_namespaceObject.getContextMenuItems)({
                isParallelGroup: isParallel,
                groupIndex,
                tasksLength: sequentialTaskGroups.length,
                groupIndexInAllTasks,
                taskIndexInAllTasks,
                isAboveParallel: (sequentialTaskGroups[groupIndex - 1]?.length ?? 0) > 1,
                isBelowParallel: (sequentialTaskGroups[groupIndex + 1]?.length ?? 0) > 1,
                reGroupTaskFunction: handleTaskRegroup,
                hideParallelOptions
            });
            return [
                ...items,
                ...reGroupOptions
            ];
        }
        return items;
    }, [
        onTaskGroupModification,
        sequentialTaskGroups,
        hideParallelOptions,
        handleTaskRegroup,
        generateReplaceTaskMenuItemForTask,
        allTasks
    ]);
    const sensors = (0, core_namespaceObject.useSensors)((0, core_namespaceObject.useSensor)(core_namespaceObject.PointerSensor, {
        activationConstraint: {
            distance: 3
        }
    }), (0, core_namespaceObject.useSensor)(core_namespaceObject.KeyboardSensor, {
        coordinateGetter: sortable_namespaceObject.sortableKeyboardCoordinates
    }));
    if (0 === sequentialTaskGroups.length) return null;
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(core_namespaceObject.DndContext, {
        collisionDetection: core_namespaceObject.closestCenter,
        sensors: sensors,
        onDragStart: handleDragStart,
        onDragMove: handleDragMove,
        onDragOver: handleDragOver,
        onDragEnd: handleDragEnd,
        onDragCancel: handleDragCancel,
        children: [
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(sortable_namespaceObject.SortableContext, {
                items: sequentialTaskIds,
                strategy: sortable_namespaceObject.verticalListSortingStrategy,
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_StageNode_styles_cjs_namespaceObject.StageTaskList, {
                    className: "nodrag nopan",
                    children: sequentialTaskGroups.map((taskGroup, groupIndex)=>{
                        const isParallel = taskGroup.length > 1;
                        return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(index_cjs_namespaceObject.Row, {
                            gap: apollo_core_namespaceObject.Spacing.SpacingS,
                            children: [
                                isParallel && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_StageNode_styles_cjs_namespaceObject.StageParallelBracket, {}),
                                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_StageNode_styles_cjs_namespaceObject.StageTaskGroupContainer, {
                                    isParallel: isParallel,
                                    children: [
                                        isParallel && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_StageNode_styles_cjs_namespaceObject.StageParallelLabel, {
                                            children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
                                                className: "text-xs",
                                                children: "Parallel"
                                            })
                                        }),
                                        taskGroup.map((task, taskIndex)=>{
                                            const taskExecution = execution?.taskStatus?.[task.id];
                                            return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_DraggableTask_cjs_namespaceObject.DraggableTask, {
                                                task: task,
                                                taskExecution: taskExecution,
                                                isSelected: selectedTaskId === task.id,
                                                isParallel: isParallel,
                                                groupIndex: groupIndex,
                                                taskIndex: taskIndex,
                                                onTaskClick: handleTaskClick,
                                                projectedDepth: task.id === activeDragId && projected ? projected.depth : void 0,
                                                isDragDisabled: !onTaskReorder || isReadOnly,
                                                isTaskLoading: loadingTaskIds?.has(task.id),
                                                ...hasContextMenu && !isReadOnly && {
                                                    getContextMenuItems: buildContextMenuItems
                                                },
                                                isReadOnly: isReadOnly
                                            }, task.id);
                                        })
                                    ]
                                })
                            ]
                        }, `group-${groupIndex}`);
                    })
                })
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_StageTaskDragOverlay_cjs_namespaceObject.StageTaskDragOverlay, {
                activeTask: activeSequentialTask?.task,
                isActiveTaskParallel: isActiveTaskParallel,
                taskWidthStyle: taskWidthStyle
            })
        ]
    });
};
exports.StageNodeSequentialTaskGroups = __webpack_exports__.StageNodeSequentialTaskGroups;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "StageNodeSequentialTaskGroups"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
