"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__, {
    StageNode: ()=>StageNode
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const index_cjs_namespaceObject = require("../FloatingCanvasPanel/index.cjs");
const external_NodeContextMenu_index_cjs_namespaceObject = require("../NodeContextMenu/index.cjs");
const hooks_index_cjs_namespaceObject = require("../NodePropertiesPanel/hooks/index.cjs");
const external_Toolbox_index_cjs_namespaceObject = require("../Toolbox/index.cjs");
const external_StageNode_styles_cjs_namespaceObject = require("./StageNode.styles.cjs");
const external_StageNodeAllTaskGroups_cjs_namespaceObject = require("./StageNodeAllTaskGroups.cjs");
const external_StageNodeHandles_cjs_namespaceObject = require("./StageNodeHandles.cjs");
const external_StageNodeHeader_cjs_namespaceObject = require("./StageNodeHeader.cjs");
const StageNodeInner = (props)=>{
    const { dragging, selected, id, width, execution, stageDetails, addTaskLabel = 'Add task', replaceTaskLabel = 'Replace task', taskOptions = [], menuItems, pendingReplaceTask, onStageClick, onTaskAdd, onAddTaskFromToolbox, onTaskToolboxSearch, onReplaceTaskFromToolbox } = props;
    const taskWidth = width ? width - external_StageNode_styles_cjs_namespaceObject.STAGE_CONTENT_INSET : void 0;
    const allTasks = (0, external_react_namespaceObject.useMemo)(()=>stageDetails?.tasks || [], [
        stageDetails?.tasks
    ]);
    const isException = stageDetails?.isException;
    const isReadOnly = !!stageDetails?.isReadOnly;
    const selectedTaskId = stageDetails?.selectedTaskId;
    const status = execution?.stageStatus?.status;
    const [isHovered, setIsHovered] = (0, external_react_namespaceObject.useState)(false);
    const handleMouseEnter = (0, external_react_namespaceObject.useCallback)(()=>setIsHovered(true), []);
    const handleMouseLeave = (0, external_react_namespaceObject.useCallback)(()=>setIsHovered(false), []);
    const taskStateReference = (0, external_react_namespaceObject.useRef)({
        isParallel: false,
        groupIndex: -1,
        taskIndex: -1
    });
    const [isAddingTask, setIsAddingTask] = (0, external_react_namespaceObject.useState)(false);
    const [isReplacingTask, setIsReplacingTask] = (0, external_react_namespaceObject.useState)(false);
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (pendingReplaceTask) {
            const match = allTasks.flatMap((group, gi)=>group.map((task, ti)=>({
                        task,
                        groupIndex: gi,
                        taskIndex: ti
                    }))).find(({ task })=>task.id === selectedTaskId);
            if (match) {
                taskStateReference.current = {
                    isParallel: (allTasks[match.groupIndex]?.length ?? 0) > 1,
                    groupIndex: match.groupIndex,
                    taskIndex: match.taskIndex
                };
                setIsReplacingTask(true);
            }
        }
    }, [
        pendingReplaceTask,
        selectedTaskId,
        allTasks
    ]);
    (0, external_react_namespaceObject.useEffect)(()=>{
        if (false === selected) {
            setIsAddingTask(false);
            setIsReplacingTask(false);
        }
    }, [
        selected
    ]);
    const shouldShowMenu = (0, external_react_namespaceObject.useMemo)(()=>menuItems && menuItems.length > 0 && (selected || isHovered) && !isReadOnly, [
        menuItems,
        selected,
        isHovered,
        isReadOnly
    ]);
    const { setSelectedNodeId } = (0, hooks_index_cjs_namespaceObject.useSetNodeSelection)();
    const handleStageClick = (0, external_react_namespaceObject.useCallback)(()=>{
        onStageClick?.();
    }, [
        onStageClick
    ]);
    const handleTaskAddClick = (0, external_react_namespaceObject.useCallback)((event)=>{
        event.stopPropagation();
        if (onTaskAdd) onTaskAdd();
        else if (onAddTaskFromToolbox) setIsAddingTask(true);
        setSelectedNodeId(id);
    }, [
        onTaskAdd,
        onAddTaskFromToolbox,
        setSelectedNodeId,
        id
    ]);
    const handleAddTaskToolboxItemSelected = (0, external_react_namespaceObject.useCallback)((item)=>{
        onAddTaskFromToolbox?.(item);
        setIsAddingTask(false);
        setSelectedNodeId(id);
    }, [
        onAddTaskFromToolbox,
        setSelectedNodeId,
        id
    ]);
    const handleReplaceTaskToolboxItemSelected = (0, external_react_namespaceObject.useCallback)((item)=>{
        onReplaceTaskFromToolbox?.(item, taskStateReference.current.groupIndex, taskStateReference.current.taskIndex);
        setIsReplacingTask(false);
    }, [
        onReplaceTaskFromToolbox
    ]);
    const taskWidthStyle = (0, external_react_namespaceObject.useMemo)(()=>taskWidth ? {
            '--stage-task-width': `${taskWidth}px`,
            '--stage-task-width-parallel': `${taskWidth - external_StageNode_styles_cjs_namespaceObject.INDENTATION_WIDTH}px`
        } : void 0, [
        taskWidth
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
        "data-testid": `stage-${id}`,
        style: {
            position: 'relative'
        },
        onClick: handleStageClick,
        onMouseEnter: handleMouseEnter,
        onMouseLeave: handleMouseLeave,
        children: [
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(external_StageNode_styles_cjs_namespaceObject.StageContainer, {
                selected: selected,
                status: status,
                width: width,
                style: taskWidthStyle,
                children: [
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_StageNodeHeader_cjs_namespaceObject.StageNodeHeader, {
                        props: props,
                        isReadOnly: isReadOnly,
                        isException: isException,
                        status: status,
                        handleTaskAddClick: handleTaskAddClick
                    }),
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_StageNodeAllTaskGroups_cjs_namespaceObject.StageNodeAllTaskGroups, {
                        props: props,
                        isReadOnly: isReadOnly,
                        taskWidthStyle: taskWidthStyle,
                        taskStateReference: taskStateReference,
                        setSelectedNodeId: setSelectedNodeId,
                        handleTaskAddClick: handleTaskAddClick,
                        setIsReplacingTask: setIsReplacingTask
                    })
                ]
            }),
            onAddTaskFromToolbox && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.FloatingCanvasPanel, {
                open: isAddingTask,
                nodeId: id,
                offset: 15,
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_Toolbox_index_cjs_namespaceObject.Toolbox, {
                    title: addTaskLabel,
                    initialItems: taskOptions,
                    onClose: ()=>setIsAddingTask(false),
                    onItemSelect: handleAddTaskToolboxItemSelected,
                    onSearch: onTaskToolboxSearch
                })
            }),
            onReplaceTaskFromToolbox && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(index_cjs_namespaceObject.FloatingCanvasPanel, {
                open: isReplacingTask,
                nodeId: id,
                offset: 15,
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_Toolbox_index_cjs_namespaceObject.Toolbox, {
                    title: replaceTaskLabel,
                    initialItems: taskOptions,
                    onClose: ()=>setIsReplacingTask(false),
                    onItemSelect: handleReplaceTaskToolboxItemSelected,
                    onSearch: onTaskToolboxSearch
                })
            }),
            menuItems && !dragging && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_NodeContextMenu_index_cjs_namespaceObject.NodeContextMenu, {
                menuItems: menuItems,
                isVisible: shouldShowMenu
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_StageNodeHandles_cjs_namespaceObject.StageNodeHandles, {
                id: id,
                isReadOnly: isReadOnly,
                selected: selected,
                isHovered: isHovered,
                isException: isException
            })
        ]
    });
};
const StageNode = /*#__PURE__*/ (0, external_react_namespaceObject.memo)(StageNodeInner);
exports.StageNode = __webpack_exports__.StageNode;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "StageNode"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
