"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__, {
    StageNodeAllTaskGroups: ()=>StageNodeAllTaskGroups
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const apollo_core_namespaceObject = require("@uipath/apollo-core");
const index_cjs_namespaceObject = require("../../layouts/index.cjs");
const apollo_wind_namespaceObject = require("@uipath/apollo-wind");
const external_react_namespaceObject = require("react");
const external_utils_index_cjs_namespaceObject = require("../../utils/index.cjs");
const useStageTasksByGroups_cjs_namespaceObject = require("./hooks/useStageTasksByGroups.cjs");
const external_StageNode_styles_cjs_namespaceObject = require("./StageNode.styles.cjs");
const external_StageNodeAdhocTaskGroups_cjs_namespaceObject = require("./StageNodeAdhocTaskGroups.cjs");
const external_StageNodeEventDrivenTaskGroups_cjs_namespaceObject = require("./StageNodeEventDrivenTaskGroups.cjs");
const external_StageNodeSequentialTaskGroups_cjs_namespaceObject = require("./StageNodeSequentialTaskGroups.cjs");
const external_StageNodeTaskUtilities_cjs_namespaceObject = require("./StageNodeTaskUtilities.cjs");
const StageNodeAllTaskGroupsInner = ({ props, isReadOnly, taskWidthStyle, taskStateReference, setSelectedNodeId, handleTaskAddClick, setIsReplacingTask })=>{
    const { id, stageDetails, onTaskAdd, onAddTaskFromToolbox, onTaskClick, onTaskGroupModification, onTaskReorder, onReplaceTaskFromToolbox } = props;
    const allTasks = (0, external_react_namespaceObject.useMemo)(()=>stageDetails?.tasks || [], [
        stageDetails?.tasks
    ]);
    const { sequentialTaskGroups, sequentialTasks, adhocTaskGroups, adhocTasks, eventDrivenTaskGroups, eventDrivenTasks } = (0, useStageTasksByGroups_cjs_namespaceObject.useStageTasksByGroups)(allTasks);
    const selectedTaskId = stageDetails?.selectedTaskId;
    const defaultContent = stageDetails?.defaultContent || (isReadOnly ? 'No tasks' : 'Add first task');
    const handleReorderSequentialTasks = (0, external_react_namespaceObject.useCallback)((newTasks)=>{
        if (!onTaskReorder) return;
        onTaskReorder([
            ...newTasks,
            ...eventDrivenTaskGroups,
            ...adhocTaskGroups
        ]);
    }, [
        onTaskReorder,
        eventDrivenTaskGroups,
        adhocTaskGroups
    ]);
    const hasContextMenu = !!(onReplaceTaskFromToolbox || onTaskGroupModification);
    const handleTaskClick = (0, external_react_namespaceObject.useCallback)((e, taskElementId)=>{
        e.stopPropagation();
        onTaskClick?.(taskElementId);
        setSelectedNodeId(id);
    }, [
        onTaskClick,
        setSelectedNodeId,
        id
    ]);
    const generateReplaceTaskMenuItemForTask = (0, external_react_namespaceObject.useCallback)((taskId, isParallel)=>{
        if (!onReplaceTaskFromToolbox) return;
        let groupIndex;
        let taskIndex;
        for (const [allTasksGroupIndex, group] of allTasks.entries())for (const [allTasksTaskIndex, task] of group.entries())if (task.id === taskId) {
            groupIndex = allTasksGroupIndex;
            taskIndex = allTasksTaskIndex;
            break;
        }
        if (void 0 === groupIndex || void 0 === taskIndex) return;
        return (0, external_StageNodeTaskUtilities_cjs_namespaceObject.getMenuItem)('replace-task', 'Replace task', ()=>{
            taskStateReference.current = {
                isParallel,
                groupIndex,
                taskIndex
            };
            onTaskClick?.(taskId);
            setIsReplacingTask(true);
        });
    }, [
        onReplaceTaskFromToolbox,
        allTasks,
        onTaskClick,
        setIsReplacingTask,
        taskStateReference
    ]);
    const generateDeleteTaskMenuItemForTask = (0, external_react_namespaceObject.useCallback)((taskId)=>{
        if (!onTaskGroupModification) return;
        let groupIndex;
        let taskIndex;
        for (const [allTasksGroupIndex, group] of allTasks.entries())for (const [allTasksTaskIndex, task] of group.entries())if (task.id === taskId) {
            groupIndex = allTasksGroupIndex;
            taskIndex = allTasksTaskIndex;
            break;
        }
        if (void 0 === groupIndex || void 0 === taskIndex) return;
        return (0, external_StageNodeTaskUtilities_cjs_namespaceObject.getMenuItem)('remove-task', 'Delete task', ()=>onTaskGroupModification(external_utils_index_cjs_namespaceObject.GroupModificationType.REMOVE_TASK, groupIndex, taskIndex));
    }, [
        allTasks,
        onTaskGroupModification
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_StageNode_styles_cjs_namespaceObject.StageContent, {
        children: 0 === sequentialTaskGroups.length && 0 === adhocTaskGroups.length && 0 === eventDrivenTaskGroups.length ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.Column, {
            py: 2,
            children: (onTaskAdd || onAddTaskFromToolbox) && !isReadOnly ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(apollo_wind_namespaceObject.Button, {
                variant: "link",
                size: "sm",
                onClick: handleTaskAddClick,
                style: {
                    maxWidth: 'fit-content',
                    padding: 0
                },
                children: defaultContent
            }) : /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
                className: "text-xs text-foreground-muted h-9",
                children: defaultContent
            })
        }) : /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(index_cjs_namespaceObject.Column, {
            py: 2,
            children: [
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_StageNodeSequentialTaskGroups_cjs_namespaceObject.StageNodeSequentialTaskGroups, {
                    props: props,
                    sequentialTaskGroups: sequentialTaskGroups,
                    sequentialTasks: sequentialTasks,
                    isReadOnly: isReadOnly,
                    selectedTaskId: selectedTaskId,
                    taskWidthStyle: taskWidthStyle,
                    hasContextMenu: hasContextMenu,
                    handleTaskClick: handleTaskClick,
                    handleReorderSequentialTasks: handleReorderSequentialTasks,
                    allTasks: allTasks,
                    generateReplaceTaskMenuItemForTask: generateReplaceTaskMenuItemForTask
                }),
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_StageNodeEventDrivenTaskGroups_cjs_namespaceObject.StageNodeEventDrivenTaskGroups, {
                    props: props,
                    eventDrivenTasks: eventDrivenTasks,
                    isReadOnly: isReadOnly,
                    selectedTaskId: selectedTaskId,
                    marginTop: sequentialTaskGroups.length > 0 ? apollo_core_namespaceObject.Spacing.SpacingS : '0px',
                    handleTaskClick: handleTaskClick,
                    generateReplaceTaskMenuItemForTask: generateReplaceTaskMenuItemForTask,
                    generateDeleteTaskMenuItemForTask: generateDeleteTaskMenuItemForTask
                }),
                /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_StageNodeAdhocTaskGroups_cjs_namespaceObject.StageNodeAdhocTaskGroups, {
                    props: props,
                    adhocTasks: adhocTasks,
                    isReadOnly: isReadOnly,
                    selectedTaskId: selectedTaskId,
                    marginTop: sequentialTaskGroups.length > 0 || eventDrivenTaskGroups.length > 0 ? apollo_core_namespaceObject.Spacing.SpacingS : '0px',
                    handleTaskClick: handleTaskClick,
                    generateReplaceTaskMenuItemForTask: generateReplaceTaskMenuItemForTask,
                    generateDeleteTaskMenuItemForTask: generateDeleteTaskMenuItemForTask
                })
            ]
        })
    });
};
const StageNodeAllTaskGroups = /*#__PURE__*/ (0, external_react_namespaceObject.memo)(StageNodeAllTaskGroupsInner);
exports.StageNodeAllTaskGroups = __webpack_exports__.StageNodeAllTaskGroups;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "StageNodeAllTaskGroups"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
