import { Position } from "../../xyflow/react.js";
import { CONTAINER_FRAME_INSET_PX, getContainerSafeArea, isContainerNodeManifest, resolveContainerPreview } from "../../utils/container.js";
import { getOppositePosition } from "../../utils/createPreviewNode.js";
import { resolveHandles } from "../../utils/manifest-resolver.js";
import { snapToGrid } from "../../utils/NodeUtils.js";
function resolveContainerHandleGroups(groups) {
    return groups.map((group)=>{
        const boundary = group.boundary ?? 'outer';
        const position = group.position;
        return {
            ...group,
            boundary,
            connectionPosition: 'inner' === boundary ? getOppositePosition(position) : position,
            customPositionAndOffsets: 'inner' === boundary ? insetInnerGroup(group) : group.customPositionAndOffsets
        };
    });
}
function getContainerRelativeBodyCenter(containerNode) {
    const safeArea = getContainerSafeArea(containerNode);
    return {
        x: snapToGrid(safeArea.x + safeArea.width / 2),
        y: snapToGrid(safeArea.y + safeArea.height / 2)
    };
}
function insetInnerGroup(group) {
    const offsets = group.customPositionAndOffsets ?? {};
    switch(group.position){
        case Position.Left:
            return {
                ...offsets,
                left: (offsets.left ?? 0) + CONTAINER_FRAME_INSET_PX
            };
        case Position.Right:
            return {
                ...offsets,
                right: (offsets.right ?? 0) + CONTAINER_FRAME_INSET_PX
            };
        case Position.Top:
            return {
                ...offsets,
                top: (offsets.top ?? 0) + CONTAINER_FRAME_INSET_PX
            };
        case Position.Bottom:
            return {
                ...offsets,
                bottom: (offsets.bottom ?? 0) + CONTAINER_FRAME_INSET_PX
            };
        default:
            return offsets;
    }
}
function resolveContainerPreviewConnectionHandles(manifest, context) {
    if (!manifest) return null;
    const innerGroups = 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: 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 = 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 && isContainerNodeManifest(getManifestForNode(sourceNode));
    const allowedContainerId = sourceIsContainer && 'inner' !== clickedHandle.boundary ? sourceNode?.parentId : void 0;
    if (sourceIsContainer && 'inner' !== clickedHandle.boundary && !allowedContainerId) return null;
    return resolveContainerPreview({
        source,
        sourceHandleType,
        reactFlowInstance,
        replacedEdge,
        allowedContainerId,
        isContainerNode: (node)=>isContainerNodeManifest(getManifestForNode(node)),
        getContainerSafeArea: 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 || !isContainerNodeManifest(getManifestForNode(sourceNode))) return;
    const clickedHandle = resolveClickedHandle({
        source,
        reactFlowInstance,
        getManifestForNode
    });
    return clickedHandle?.boundary === 'inner' ? sourceNode.id : void 0;
}
export { getContainerRelativeBodyCenter, getInnerHandleContainerId, resolveContainerAddNodePreview, resolveContainerHandleGroups, resolveContainerPreviewConnectionHandles };
