"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__, {
    resolveContainerHandleGroups: ()=>resolveContainerHandleGroups,
    getInnerHandleContainerId: ()=>getInnerHandleContainerId,
    resolveContainerPreviewConnectionHandles: ()=>resolveContainerPreviewConnectionHandles,
    getContainerRelativeBodyCenter: ()=>getContainerRelativeBodyCenter,
    resolveContainerAddNodePreview: ()=>resolveContainerAddNodePreview
});
const react_cjs_namespaceObject = require("../../xyflow/react.cjs");
const container_cjs_namespaceObject = require("../../utils/container.cjs");
const createPreviewNode_cjs_namespaceObject = require("../../utils/createPreviewNode.cjs");
const manifest_resolver_cjs_namespaceObject = require("../../utils/manifest-resolver.cjs");
const NodeUtils_cjs_namespaceObject = require("../../utils/NodeUtils.cjs");
function resolveContainerHandleGroups(groups) {
    return groups.map((group)=>{
        const boundary = group.boundary ?? 'outer';
        const position = group.position;
        return {
            ...group,
            boundary,
            connectionPosition: 'inner' === boundary ? (0, createPreviewNode_cjs_namespaceObject.getOppositePosition)(position) : position,
            customPositionAndOffsets: 'inner' === boundary ? insetInnerGroup(group) : group.customPositionAndOffsets
        };
    });
}
function getContainerRelativeBodyCenter(containerNode) {
    const safeArea = (0, container_cjs_namespaceObject.getContainerSafeArea)(containerNode);
    return {
        x: (0, NodeUtils_cjs_namespaceObject.snapToGrid)(safeArea.x + safeArea.width / 2),
        y: (0, NodeUtils_cjs_namespaceObject.snapToGrid)(safeArea.y + safeArea.height / 2)
    };
}
function insetInnerGroup(group) {
    const offsets = group.customPositionAndOffsets ?? {};
    switch(group.position){
        case react_cjs_namespaceObject.Position.Left:
            return {
                ...offsets,
                left: (offsets.left ?? 0) + container_cjs_namespaceObject.CONTAINER_FRAME_INSET_PX
            };
        case react_cjs_namespaceObject.Position.Right:
            return {
                ...offsets,
                right: (offsets.right ?? 0) + container_cjs_namespaceObject.CONTAINER_FRAME_INSET_PX
            };
        case react_cjs_namespaceObject.Position.Top:
            return {
                ...offsets,
                top: (offsets.top ?? 0) + container_cjs_namespaceObject.CONTAINER_FRAME_INSET_PX
            };
        case react_cjs_namespaceObject.Position.Bottom:
            return {
                ...offsets,
                bottom: (offsets.bottom ?? 0) + container_cjs_namespaceObject.CONTAINER_FRAME_INSET_PX
            };
        default:
            return offsets;
    }
}
function resolveContainerPreviewConnectionHandles(manifest, context) {
    if (!manifest) return null;
    const innerGroups = (0, manifest_resolver_cjs_namespaceObject.resolveHandles)(manifest.handleConfiguration, context).filter((group)=>'inner' === group.boundary && (group.visible ?? true));
    const sourceHandle = pickPreferredInnerHandle(innerGroups, 'source');
    const targetHandle = pickPreferredInnerHandle(innerGroups, 'target');
    if (!sourceHandle || !targetHandle) return null;
    return {
        sourceHandleId: sourceHandle.handle.id,
        sourceHandlePosition: (0, createPreviewNode_cjs_namespaceObject.getOppositePosition)(sourceHandle.group.position),
        targetHandleId: targetHandle.handle.id
    };
}
function pickPreferredInnerHandle(groups, type) {
    for (const group of groups){
        const handle = group.handles.find((candidate)=>candidate.type === type && candidate.visible);
        if (handle) return {
            group,
            handle
        };
    }
    return null;
}
function resolveClickedHandle({ source, reactFlowInstance, getManifestForNode }) {
    if (!source.handleId) return;
    const sourceNode = reactFlowInstance.getNode(source.nodeId);
    if (!sourceNode) return;
    const sourceManifest = getManifestForNode(sourceNode);
    const dataHandleConfiguration = sourceNode.data?.handleConfigurations;
    const handleConfiguration = Array.isArray(dataHandleConfiguration) ? dataHandleConfiguration : sourceManifest?.handleConfiguration;
    if (!handleConfiguration) return;
    const sourceHandles = (0, manifest_resolver_cjs_namespaceObject.resolveHandles)(handleConfiguration, {
        ...sourceNode.data,
        nodeId: sourceNode.id
    });
    for (const group of sourceHandles){
        const handle = group.handles.find((candidate)=>candidate.id === source.handleId);
        if (handle) return {
            boundary: group.boundary ?? 'outer',
            handleType: handle.handleType
        };
    }
}
function resolveContainerAddNodePreview({ source, sourceHandleType, reactFlowInstance, getManifestForNode, replacedEdge }) {
    const clickedHandle = resolveClickedHandle({
        source,
        reactFlowInstance,
        getManifestForNode
    });
    if (clickedHandle?.handleType !== 'output') return null;
    const sourceNode = reactFlowInstance.getNode(source.nodeId);
    const sourceIsContainer = void 0 !== sourceNode && (0, container_cjs_namespaceObject.isContainerNodeManifest)(getManifestForNode(sourceNode));
    if (sourceIsContainer && 'inner' !== clickedHandle.boundary) return null;
    return (0, container_cjs_namespaceObject.resolveContainerPreview)({
        source,
        sourceHandleType,
        reactFlowInstance,
        replacedEdge,
        isContainerNode: (node)=>(0, container_cjs_namespaceObject.isContainerNodeManifest)(getManifestForNode(node)),
        getContainerSafeArea: container_cjs_namespaceObject.getContainerSafeArea,
        getContainerContinuationTarget: ({ containerNode })=>{
            const previewHandles = resolveContainerPreviewConnectionHandles(getManifestForNode(containerNode), {
                ...containerNode.data,
                nodeId: containerNode.id
            });
            return previewHandles ? {
                nodeId: containerNode.id,
                handleId: previewHandles.targetHandleId
            } : null;
        }
    });
}
function getInnerHandleContainerId({ source, reactFlowInstance, getManifestForNode }) {
    const sourceNode = reactFlowInstance.getNode(source.nodeId);
    if (!sourceNode || !(0, container_cjs_namespaceObject.isContainerNodeManifest)(getManifestForNode(sourceNode))) return;
    const clickedHandle = resolveClickedHandle({
        source,
        reactFlowInstance,
        getManifestForNode
    });
    return clickedHandle?.boundary === 'inner' ? sourceNode.id : void 0;
}
exports.getContainerRelativeBodyCenter = __webpack_exports__.getContainerRelativeBodyCenter;
exports.getInnerHandleContainerId = __webpack_exports__.getInnerHandleContainerId;
exports.resolveContainerAddNodePreview = __webpack_exports__.resolveContainerAddNodePreview;
exports.resolveContainerHandleGroups = __webpack_exports__.resolveContainerHandleGroups;
exports.resolveContainerPreviewConnectionHandles = __webpack_exports__.resolveContainerPreviewConnectionHandles;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "getContainerRelativeBodyCenter",
    "getInnerHandleContainerId",
    "resolveContainerAddNodePreview",
    "resolveContainerHandleGroups",
    "resolveContainerPreviewConnectionHandles"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
