import { jsx, jsxs } from "react/jsx-runtime";
import { memo, useCallback } from "react";
import { AdhocTaskItem } from "./AdhocTask.js";
import { StageAdditionalTasksHeaderSection, StageAdditionalTasksSection, StageTaskList } from "./StageNode.styles.js";
import { useStageNodeLabels } from "./useStageNodeLabels.js";
const StageNodeAdhocTaskGroupsInner = ({ props, adhocTasks, isReadOnly, selectedTaskId, marginTop, handleTaskClick, generateReplaceTaskMenuItemForTask, generateDeleteTaskMenuItemForTask })=>{
    const { execution, onTaskGroupModification, onReplaceTaskFromToolbox, onTaskPlay, loadingTaskIds, getTaskContextMenuItems } = props;
    const hasBuiltInTaskActions = !!(onReplaceTaskFromToolbox || onTaskGroupModification);
    const labels = useStageNodeLabels();
    const getAdhocContextMenuItems = useCallback((task)=>{
        const items = [];
        const replaceTaskMenuItem = generateReplaceTaskMenuItemForTask(task.id, false);
        if (replaceTaskMenuItem) items.push(replaceTaskMenuItem);
        const additionalMenuItems = getTaskContextMenuItems?.({
            task,
            taskGroupType: 'adhoc',
            isParallel: false
        }) ?? [];
        items.push(...additionalMenuItems);
        const deleteTaskMenuItem = generateDeleteTaskMenuItemForTask(task.id);
        if (deleteTaskMenuItem) items.push(deleteTaskMenuItem);
        return items;
    }, [
        generateReplaceTaskMenuItemForTask,
        getTaskContextMenuItems,
        generateDeleteTaskMenuItemForTask
    ]);
    if (0 === adhocTasks.length) return null;
    return /*#__PURE__*/ jsxs(StageAdditionalTasksSection, {
        style: {
            marginTop
        },
        children: [
            /*#__PURE__*/ jsx(StageAdditionalTasksHeaderSection, {
                children: /*#__PURE__*/ jsx("span", {
                    className: "text-xs font-bold text-foreground-muted",
                    children: labels.adhocTasks
                })
            }),
            /*#__PURE__*/ jsx(StageTaskList, {
                children: adhocTasks.map(({ task })=>{
                    const taskExecution = execution?.taskStatus?.[task.id];
                    const customItems = isReadOnly || hasBuiltInTaskActions ? [] : getTaskContextMenuItems?.({
                        task,
                        taskGroupType: 'adhoc',
                        isParallel: false
                    }) ?? [];
                    const hasMenu = !isReadOnly && (hasBuiltInTaskActions || customItems.length > 0);
                    return /*#__PURE__*/ jsx(AdhocTaskItem, {
                        task: task,
                        taskExecution: taskExecution,
                        isSelected: selectedTaskId === task.id,
                        onTaskClick: handleTaskClick,
                        onTaskPlay: onTaskPlay,
                        isTaskLoading: loadingTaskIds?.has(task.id),
                        getContextMenuItems: hasMenu ? getAdhocContextMenuItems : void 0
                    }, task.id);
                })
            })
        ]
    });
};
const StageNodeAdhocTaskGroups = /*#__PURE__*/ memo(StageNodeAdhocTaskGroupsInner);
export { StageNodeAdhocTaskGroups };
