import { jsx, jsxs } from "react/jsx-runtime";
import { Spacing } from "@uipath/apollo-core";
import { Column } from "../../layouts/index.js";
import { Button } from "@uipath/apollo-wind";
import { memo, useCallback, useMemo } from "react";
import { GroupModificationType } from "../../utils/index.js";
import { useStageTasksByGroups } from "./hooks/useStageTasksByGroups.js";
import { StageContent } from "./StageNode.styles.js";
import { StageNodeAdhocTaskGroups } from "./StageNodeAdhocTaskGroups.js";
import { StageNodeEventDrivenTaskGroups } from "./StageNodeEventDrivenTaskGroups.js";
import { StageNodeSequentialTaskGroups } from "./StageNodeSequentialTaskGroups.js";
import { getMenuItem } from "./StageNodeTaskUtilities.js";
const StageNodeAllTaskGroupsInner = ({ props, isReadOnly, taskWidthStyle, taskStateReference, setSelectedNodeId, handleTaskAddClick, setIsReplacingTask })=>{
    const { id, stageDetails, onTaskAdd, onAddTaskFromToolbox, onTaskClick, onTaskGroupModification, onTaskReorder, onReplaceTaskFromToolbox } = props;
    const allTasks = useMemo(()=>stageDetails?.tasks || [], [
        stageDetails?.tasks
    ]);
    const { sequentialTaskGroups, sequentialTasks, adhocTaskGroups, adhocTasks, eventDrivenTaskGroups, eventDrivenTasks } = useStageTasksByGroups(allTasks);
    const selectedTaskId = stageDetails?.selectedTaskId;
    const defaultContent = stageDetails?.defaultContent || (isReadOnly ? 'No tasks' : 'Add first task');
    const handleReorderSequentialTasks = useCallback((newTasks)=>{
        if (!onTaskReorder) return;
        onTaskReorder([
            ...newTasks,
            ...eventDrivenTaskGroups,
            ...adhocTaskGroups
        ]);
    }, [
        onTaskReorder,
        eventDrivenTaskGroups,
        adhocTaskGroups
    ]);
    const hasContextMenu = !!(onReplaceTaskFromToolbox || onTaskGroupModification);
    const handleTaskClick = useCallback((e, taskElementId)=>{
        e.stopPropagation();
        onTaskClick?.(taskElementId);
        setSelectedNodeId(id);
    }, [
        onTaskClick,
        setSelectedNodeId,
        id
    ]);
    const generateReplaceTaskMenuItemForTask = 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 getMenuItem('replace-task', 'Replace task', ()=>{
            taskStateReference.current = {
                isParallel,
                groupIndex,
                taskIndex
            };
            onTaskClick?.(taskId);
            setIsReplacingTask(true);
        });
    }, [
        onReplaceTaskFromToolbox,
        allTasks,
        onTaskClick,
        setIsReplacingTask,
        taskStateReference
    ]);
    const generateDeleteTaskMenuItemForTask = 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 getMenuItem('remove-task', 'Delete task', ()=>onTaskGroupModification(GroupModificationType.REMOVE_TASK, groupIndex, taskIndex));
    }, [
        allTasks,
        onTaskGroupModification
    ]);
    return /*#__PURE__*/ jsx(StageContent, {
        children: 0 === sequentialTaskGroups.length && 0 === adhocTaskGroups.length && 0 === eventDrivenTaskGroups.length ? /*#__PURE__*/ jsx(Column, {
            py: 2,
            children: (onTaskAdd || onAddTaskFromToolbox) && !isReadOnly ? /*#__PURE__*/ jsx(Button, {
                variant: "link",
                size: "sm",
                onClick: handleTaskAddClick,
                style: {
                    maxWidth: 'fit-content',
                    padding: 0
                },
                children: defaultContent
            }) : /*#__PURE__*/ jsx("span", {
                className: "text-xs text-foreground-muted h-9",
                children: defaultContent
            })
        }) : /*#__PURE__*/ jsxs(Column, {
            py: 2,
            children: [
                /*#__PURE__*/ jsx(StageNodeSequentialTaskGroups, {
                    props: props,
                    sequentialTaskGroups: sequentialTaskGroups,
                    sequentialTasks: sequentialTasks,
                    isReadOnly: isReadOnly,
                    selectedTaskId: selectedTaskId,
                    taskWidthStyle: taskWidthStyle,
                    hasContextMenu: hasContextMenu,
                    handleTaskClick: handleTaskClick,
                    handleReorderSequentialTasks: handleReorderSequentialTasks,
                    allTasks: allTasks,
                    generateReplaceTaskMenuItemForTask: generateReplaceTaskMenuItemForTask
                }),
                /*#__PURE__*/ jsx(StageNodeEventDrivenTaskGroups, {
                    props: props,
                    eventDrivenTasks: eventDrivenTasks,
                    isReadOnly: isReadOnly,
                    selectedTaskId: selectedTaskId,
                    marginTop: sequentialTaskGroups.length > 0 ? Spacing.SpacingS : '0px',
                    handleTaskClick: handleTaskClick,
                    generateReplaceTaskMenuItemForTask: generateReplaceTaskMenuItemForTask,
                    generateDeleteTaskMenuItemForTask: generateDeleteTaskMenuItemForTask
                }),
                /*#__PURE__*/ jsx(StageNodeAdhocTaskGroups, {
                    props: props,
                    adhocTasks: adhocTasks,
                    isReadOnly: isReadOnly,
                    selectedTaskId: selectedTaskId,
                    marginTop: sequentialTaskGroups.length > 0 || eventDrivenTaskGroups.length > 0 ? Spacing.SpacingS : '0px',
                    handleTaskClick: handleTaskClick,
                    generateReplaceTaskMenuItemForTask: generateReplaceTaskMenuItemForTask,
                    generateDeleteTaskMenuItemForTask: generateDeleteTaskMenuItemForTask
                })
            ]
        })
    });
};
const StageNodeAllTaskGroups = /*#__PURE__*/ memo(StageNodeAllTaskGroupsInner);
export { StageNodeAllTaskGroups };
