"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__, {
    LoopNode: ()=>LoopNode
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const react_cjs_namespaceObject = require("../../xyflow/react.cjs");
const apollo_wind_namespaceObject = require("@uipath/apollo-wind");
const external_react_namespaceObject = require("react");
const index_cjs_namespaceObject = require("../../core/index.cjs");
const external_hooks_index_cjs_namespaceObject = require("../../hooks/index.cjs");
const adornment_resolver_cjs_namespaceObject = require("../../utils/adornment-resolver.cjs");
const container_cjs_namespaceObject = require("../../utils/container.cjs");
const icon_registry_cjs_namespaceObject = require("../../utils/icon-registry.cjs");
const manifest_resolver_cjs_namespaceObject = require("../../utils/manifest-resolver.cjs");
const NodeUtils_cjs_namespaceObject = require("../../utils/NodeUtils.cjs");
const toolbar_resolver_cjs_namespaceObject = require("../../utils/toolbar-resolver.cjs");
const BaseCanvasModeProvider_cjs_namespaceObject = require("../BaseCanvas/BaseCanvasModeProvider.cjs");
const ConnectedHandlesContext_cjs_namespaceObject = require("../BaseCanvas/ConnectedHandlesContext.cjs");
const SelectionStateContext_cjs_namespaceObject = require("../BaseCanvas/SelectionStateContext.cjs");
const BaseNodeBadgeSlot_cjs_namespaceObject = require("../BaseNode/BaseNodeBadgeSlot.cjs");
const BaseNodeContainer_cjs_namespaceObject = require("../BaseNode/BaseNodeContainer.cjs");
const BaseNodeMissingManifest_cjs_namespaceObject = require("../BaseNode/BaseNodeMissingManifest.cjs");
const external_ButtonHandle_index_cjs_namespaceObject = require("../ButtonHandle/index.cjs");
const external_Toolbar_index_cjs_namespaceObject = require("../Toolbar/index.cjs");
const external_LoopNode_helpers_cjs_namespaceObject = require("./LoopNode.helpers.cjs");
const DEFAULT_LOOP_ICON = 'repeat';
const DEFAULT_LOOP_TITLE = 'Loop';
const EMPTY_DATA = {};
const RESIZE_CONTROLS = [
    {
        position: 'top-left',
        cursor: 'nwse-resize',
        indicatorClassName: 'top-[-4px] left-[-4px]'
    },
    {
        position: 'top-right',
        cursor: 'nesw-resize',
        indicatorClassName: 'top-[-4px] right-[-4px]'
    },
    {
        position: 'bottom-left',
        cursor: 'nesw-resize',
        indicatorClassName: 'bottom-[-4px] left-[-4px]'
    },
    {
        position: 'bottom-right',
        cursor: 'nwse-resize',
        indicatorClassName: 'bottom-[-4px] right-[-4px]'
    }
];
const RESIZE_CONTROL_STYLE = {
    background: 'transparent',
    border: 'none',
    zIndex: 100
};
const ADORNMENT_SLOT_POSITIONS = [
    'topLeft',
    'topRight',
    'bottomLeft',
    'bottomRight'
];
const ADORNMENT_SLOT_SHAPES = {
    topLeft: 'top-left',
    topRight: 'top-right',
    bottomLeft: 'bottom-left',
    bottomRight: 'bottom-right'
};
function resolveInteractionState(dragging, selected, isHovered) {
    if (dragging) return 'drag';
    if (selected) return 'selected';
    if (isHovered) return 'hover';
    return 'default';
}
function useHasChildNodes(id, enabled) {
    return (0, react_cjs_namespaceObject.useStore)((0, external_react_namespaceObject.useCallback)((state)=>!enabled || state.nodes.some((node)=>node.parentId === id), [
        id,
        enabled
    ]));
}
function useContainerNodeInternalsRefresh(id, handleGroups, width, height) {
    const updateNodeInternals = (0, react_cjs_namespaceObject.useUpdateNodeInternals)();
    (0, external_react_namespaceObject.useEffect)(()=>{
        const frameId = requestAnimationFrame(()=>{
            updateNodeInternals(id);
        });
        return ()=>{
            cancelAnimationFrame(frameId);
        };
    }, [
        id,
        handleGroups,
        updateNodeInternals,
        width,
        height
    ]);
}
function LoopNodeComponent(props) {
    const { id, type, data, selected = false, dragging = false, width = 0, height = 0, onAddFirstChild, onResize, toolbarConfig: toolbarConfigProp, adornments: adornmentsProp, executionStatusOverride, suggestionType: suggestionTypeProp } = props;
    const nodeTypeRegistry = (0, index_cjs_namespaceObject.useOptionalNodeTypeRegistry)();
    const [isHovered, setIsHovered] = (0, external_react_namespaceObject.useState)(false);
    const resolvedData = data ?? EMPTY_DATA;
    const isLoading = !!resolvedData.loading;
    const suggestionType = suggestionTypeProp ?? resolvedData.suggestionType;
    const manifest = (0, external_react_namespaceObject.useMemo)(()=>nodeTypeRegistry?.getManifest(type), [
        nodeTypeRegistry,
        type
    ]);
    const { mode } = (0, BaseCanvasModeProvider_cjs_namespaceObject.useBaseCanvasMode)();
    const isDesignMode = 'design' === mode;
    const connectedHandleIds = (0, ConnectedHandlesContext_cjs_namespaceObject.useConnectedHandles)(id);
    const { multipleNodesSelected } = (0, SelectionStateContext_cjs_namespaceObject.useSelectionState)();
    const isConnecting = (0, react_cjs_namespaceObject.useStore)(NodeUtils_cjs_namespaceObject.selectIsConnecting);
    const hasChildNodes = useHasChildNodes(id, isDesignMode && !!onAddFirstChild);
    const executionState = (0, external_hooks_index_cjs_namespaceObject.useNodeExecutionState)(id);
    const validationState = (0, external_hooks_index_cjs_namespaceObject.useElementValidationStatus)(id);
    const statusContext = (0, external_react_namespaceObject.useMemo)(()=>({
            nodeId: id,
            executionState: executionStatusOverride ?? executionState,
            validationState,
            isConnecting,
            isSelected: selected,
            isDragging: dragging,
            mode
        }), [
        dragging,
        executionStatusOverride,
        executionState,
        id,
        isConnecting,
        mode,
        selected,
        validationState
    ]);
    const executionStatus = executionStatusOverride ?? ('string' == typeof executionState ? executionState : executionState?.status);
    const display = (0, external_react_namespaceObject.useMemo)(()=>(0, manifest_resolver_cjs_namespaceObject.resolveDisplay)(manifest?.display, {
            ...resolvedData,
            nodeId: id
        }), [
        manifest?.display,
        id,
        resolvedData
    ]);
    const displayTitle = display.label ?? DEFAULT_LOOP_TITLE;
    const displayIcon = display.icon ?? DEFAULT_LOOP_ICON;
    const isParallel = true === resolvedData.parallel;
    const isDropTarget = true === resolvedData.isDropTarget;
    const containerWidth = width || container_cjs_namespaceObject.DEFAULT_CONTAINER_WIDTH;
    const containerHeight = height || container_cjs_namespaceObject.DEFAULT_CONTAINER_HEIGHT;
    const nodeSizeStyle = {
        width: containerWidth,
        height: containerHeight,
        minWidth: container_cjs_namespaceObject.DEFAULT_CONTAINER_MIN_WIDTH,
        minHeight: container_cjs_namespaceObject.DEFAULT_CONTAINER_MIN_HEIGHT
    };
    const toolbarConfig = (0, external_react_namespaceObject.useMemo)(()=>{
        if (void 0 !== toolbarConfigProp) return null === toolbarConfigProp ? void 0 : toolbarConfigProp;
        return manifest ? (0, toolbar_resolver_cjs_namespaceObject.resolveToolbar)(manifest, statusContext, data) : void 0;
    }, [
        data,
        manifest,
        statusContext,
        toolbarConfigProp
    ]);
    const adornments = (0, external_react_namespaceObject.useMemo)(()=>({
            ...(0, adornment_resolver_cjs_namespaceObject.resolveAdornments)(statusContext),
            ...adornmentsProp ?? {}
        }), [
        adornmentsProp,
        statusContext
    ]);
    const resolvedHandleGroups = (0, external_react_namespaceObject.useMemo)(()=>manifest ? (0, manifest_resolver_cjs_namespaceObject.resolveHandles)(manifest.handleConfiguration, resolvedData) : [], [
        manifest,
        resolvedData
    ]);
    const containerHandleGroups = (0, external_react_namespaceObject.useMemo)(()=>(0, external_LoopNode_helpers_cjs_namespaceObject.resolveContainerHandleGroups)(resolvedHandleGroups), [
        resolvedHandleGroups
    ]);
    useContainerNodeInternalsRefresh(id, containerHandleGroups, containerWidth, containerHeight);
    const handleResize = (0, external_react_namespaceObject.useCallback)((_event, params)=>{
        onResize?.({
            width: (0, NodeUtils_cjs_namespaceObject.snapToGrid)(params.width),
            height: (0, NodeUtils_cjs_namespaceObject.snapToGrid)(params.height)
        });
    }, [
        onResize
    ]);
    const handleEmptyClick = (0, external_react_namespaceObject.useCallback)(()=>{
        onAddFirstChild?.();
    }, [
        onAddFirstChild
    ]);
    const handleMouseEnter = (0, external_react_namespaceObject.useCallback)(()=>setIsHovered(true), []);
    const handleMouseLeave = (0, external_react_namespaceObject.useCallback)(()=>setIsHovered(false), []);
    const handleHandleAction = (0, external_react_namespaceObject.useCallback)((_event)=>{
        setIsHovered(false);
    }, []);
    const shouldShowHandles = (isConnecting || selected || isHovered) && !dragging;
    const showHandleAddButtons = isDesignMode && !multipleNodesSelected && !isConnecting && !dragging;
    const showResizeControls = selected && !dragging && isDesignMode;
    const showEmptyStateButton = isDesignMode && !hasChildNodes && !!onAddFirstChild;
    const interactionState = resolveInteractionState(dragging, selected, isHovered);
    if (!manifest) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        className: "relative",
        style: nodeSizeStyle,
        onMouseEnter: handleMouseEnter,
        onMouseLeave: handleMouseLeave,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(BaseNodeMissingManifest_cjs_namespaceObject.MissingManifestNode, {
            type: type,
            isSelected: selected,
            isHovered: isHovered,
            interactionState: interactionState
        })
    });
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
        "data-loop-container": true,
        "data-selected": selected ? 'true' : 'false',
        "data-execution-status": executionStatus,
        "data-interaction-state": interactionState,
        "data-suggestion-type": suggestionType,
        "data-validation-status": validationState?.validationStatus,
        "aria-busy": resolvedData.loading || void 0,
        className: (0, apollo_wind_namespaceObject.cn)('group/loop-shell relative box-border flex h-full w-full flex-col overflow-visible rounded-[20px] border bg-transparent', 'transition-[border-color,box-shadow,opacity] shadow-(--canvas-node-shadow-rest)', 'border-border-subtle', (0, BaseNodeContainer_cjs_namespaceObject.getStatusBorder)(suggestionType ?? validationState?.validationStatus ?? executionStatus), isHovered && 'shadow-(--canvas-node-shadow-hover) border-border-hover', selected && 'outline outline-2 outline-foreground-accent-muted', isDropTarget && 'bg-surface-hover outline outline-2 outline-brand', 'drag' === interactionState && 'cursor-grabbing shadow-(--canvas-node-shadow-lifted)'),
        style: {
            ...nodeSizeStyle,
            ...display.background ? {
                background: display.background
            } : {}
        },
        onMouseEnter: handleMouseEnter,
        onMouseLeave: handleMouseLeave,
        children: [
            ADORNMENT_SLOT_POSITIONS.map((slot)=>adornments?.[slot] ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(BaseNodeBadgeSlot_cjs_namespaceObject.BaseBadgeSlot, {
                    position: ADORNMENT_SLOT_SHAPES[slot],
                    shape: "rectangle",
                    children: adornments[slot]
                }, slot) : null),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(ResizeCornerIndicators, {
                visible: showResizeControls
            }),
            showResizeControls ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(ResizeControls, {
                onResize: handleResize
            }) : null,
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(Header, {
                title: displayTitle,
                icon: displayIcon,
                loading: isLoading,
                isParallel: isParallel
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(BodyFrame, {
                isEmpty: showEmptyStateButton,
                isLoading: isLoading
            }),
            showEmptyStateButton ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                className: (0, apollo_wind_namespaceObject.cn)('pointer-events-none absolute left-1/2 top-1/2', '-translate-x-1/2 -translate-y-1/2'),
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(EmptyState, {
                    onAddFirstChild: handleEmptyClick
                })
            }) : null,
            toolbarConfig && /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_Toolbar_index_cjs_namespaceObject.NodeToolbar, {
                nodeId: id,
                config: toolbarConfig,
                expanded: selected || isHovered,
                hidden: dragging || multipleNodesSelected,
                portalToNodeOverlay: true
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(HandleGroups, {
                nodeId: id,
                groups: containerHandleGroups,
                selected: selected,
                hovered: isHovered,
                shouldShowHandles: shouldShowHandles,
                showAddButton: showHandleAddButtons,
                showNotches: shouldShowHandles,
                nodeWidth: containerWidth,
                nodeHeight: containerHeight,
                connectedHandleIds: connectedHandleIds,
                onHandleAction: handleHandleAction
            })
        ]
    });
}
const LoopNode = /*#__PURE__*/ (0, external_react_namespaceObject.memo)(LoopNodeComponent);
function Header({ title, icon, loading, isParallel }) {
    const titleContent = loading ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        className: "h-5 w-28 animate-pulse rounded bg-(--canvas-background-overlay)"
    }) : /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
        className: "truncate text-[15px] font-semibold leading-5 tracking-normal",
        children: title
    });
    const iconContent = loading ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        className: "h-4 w-4 shrink-0 animate-pulse rounded bg-(--canvas-background-overlay)"
    }) : icon ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
        className: "shrink-0 text-foreground",
        "aria-hidden": true,
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icon_registry_cjs_namespaceObject.CanvasIcon, {
            icon: icon,
            size: 16
        })
    }) : null;
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
        className: "flex shrink-0 cursor-grab items-center justify-between gap-2.5 px-3.5 pt-2.5 text-foreground active:cursor-grabbing",
        "data-testid": "loop-node-header",
        children: [
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("div", {
                className: "flex min-w-0 items-center gap-2.5",
                children: [
                    iconContent,
                    titleContent
                ]
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("span", {
                className: "flex shrink-0 items-center gap-1 rounded-full border border-border-subtle bg-transparent px-2.5 py-0.5 text-[11px] font-semibold leading-4 text-foreground",
                children: [
                    /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
                        className: (0, apollo_wind_namespaceObject.cn)('flex shrink-0', isParallel && 'rotate-90'),
                        "aria-hidden": true,
                        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icon_registry_cjs_namespaceObject.CanvasIcon, {
                            icon: "align-justify",
                            size: 11
                        })
                    }),
                    isParallel ? 'Parallel' : 'Sequential'
                ]
            })
        ]
    });
}
function EmptyState({ onAddFirstChild }) {
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("button", {
        type: "button",
        onClick: onAddFirstChild,
        "aria-label": "Add node to loop",
        className: (0, apollo_wind_namespaceObject.cn)('nodrag nopan', 'pointer-events-auto flex h-8 w-8 items-center justify-center rounded-xl', 'border border-border bg-surface-overlay text-foreground', 'shadow-(--canvas-node-shadow-lifted)', 'transition-colors', 'hover:bg-surface-hover hover:border-brand'),
        children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(icon_registry_cjs_namespaceObject.CanvasIcon, {
            icon: "plus",
            size: 14
        })
    });
}
function BodyFrame({ isEmpty, isLoading }) {
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        "data-testid": "loop-body-frame",
        "data-empty": isEmpty ? 'true' : 'false',
        className: (0, apollo_wind_namespaceObject.cn)('relative m-2.5 flex flex-1 rounded-xl border-[1.5px] border-dashed border-border-subtle bg-transparent', 'pointer-events-none'),
        children: isLoading ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
            className: "m-6 h-14 w-full animate-pulse rounded-[18px] bg-(--canvas-background-overlay)"
        }) : null
    });
}
function ResizeControls({ onResize }) {
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(jsx_runtime_namespaceObject.Fragment, {
        children: RESIZE_CONTROLS.map(({ position, cursor })=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(react_cjs_namespaceObject.NodeResizeControl, {
                style: RESIZE_CONTROL_STYLE,
                position: position,
                minWidth: container_cjs_namespaceObject.DEFAULT_CONTAINER_MIN_WIDTH,
                minHeight: container_cjs_namespaceObject.DEFAULT_CONTAINER_MIN_HEIGHT,
                onResize: onResize,
                children: /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                    className: "absolute bottom-0 right-0 h-5 w-5 pointer-events-auto",
                    style: {
                        cursor
                    }
                })
            }, position))
    });
}
function ResizeCornerIndicators({ visible }) {
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(jsx_runtime_namespaceObject.Fragment, {
        children: RESIZE_CONTROLS.map(({ position, indicatorClassName })=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
                "aria-hidden": true,
                className: (0, apollo_wind_namespaceObject.cn)('pointer-events-none absolute h-2 w-2 rounded-full bg-brand transition-opacity', indicatorClassName, visible ? 'opacity-100' : 'opacity-0')
            }, position))
    });
}
function HandleGroups({ groups, ...handleGroupProps }) {
    if (0 === groups.length) return null;
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(jsx_runtime_namespaceObject.Fragment, {
        children: groups.map((group, groupIndex)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(HandleGroup, {
                ...handleGroupProps,
                group: group
            }, `${group.boundary}:${group.position}:${groupIndex}`))
    });
}
function HandleGroup({ nodeId, group, selected, hovered, shouldShowHandles, showAddButton, showNotches, nodeWidth, nodeHeight, connectedHandleIds, onHandleAction }) {
    const groupVisible = shouldShowHandles && (group.visible ?? true);
    const position = group.position;
    const enhancedHandles = (0, external_react_namespaceObject.useMemo)(()=>group.handles.map((handle)=>{
            const showHandle = connectedHandleIds.has(handle.id) || groupVisible;
            if ('inner' === group.boundary) return {
                ...handle,
                showHandle,
                showButton: false,
                onAction: void 0
            };
            return {
                ...handle,
                showHandle,
                showButton: handle.showButton,
                onAction: handle.onAction ?? onHandleAction
            };
        }), [
        group.boundary,
        group.handles,
        connectedHandleIds,
        groupVisible,
        onHandleAction
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_ButtonHandle_index_cjs_namespaceObject.ButtonHandles, {
        nodeId: nodeId,
        handles: enhancedHandles,
        position: position,
        connectionPosition: group.connectionPosition,
        selected: selected,
        hovered: hovered,
        showAddButton: showAddButton,
        showNotches: showNotches,
        customPositionAndOffsets: group.customPositionAndOffsets,
        nodeWidth: nodeWidth,
        nodeHeight: nodeHeight,
        portalActions: 'outer' === group.boundary
    });
}
exports.LoopNode = __webpack_exports__.LoopNode;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "LoopNode"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
