"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__, {
    getOriginalEdge: ()=>getOriginalEdge,
    placeAddedNode: ()=>placeAddedNode,
    alignNodeToPreview: ()=>alignNodeToPreview
});
const react_cjs_namespaceObject = require("../../xyflow/react.cjs");
const external_constants_cjs_namespaceObject = require("../../constants.cjs");
const index_cjs_namespaceObject = require("../../utils/index.cjs");
const collapse_cjs_namespaceObject = require("../../utils/collapse.cjs");
const container_cjs_namespaceObject = require("../../utils/container.cjs");
const NodeUtils_cjs_namespaceObject = require("../../utils/NodeUtils.cjs");
const TOP_LEVEL_INSERTION_GAP_PX = 5 * external_constants_cjs_namespaceObject.GRID_SPACING;
function getOriginalEdge(previewNode) {
    return previewNode?.data?.originalEdge ?? null;
}
function getPrimaryPreviewHandlePosition(previewNode, previewConnections) {
    const primaryConnection = previewConnections.find((connection)=>!connection.addNewNodeAsSource) ?? previewConnections[0];
    const handlePosition = primaryConnection?.addNewNodeAsSource ? previewNode.data?.outputHandlePosition : previewNode.data?.inputHandlePosition;
    return handlePosition;
}
function alignNodeToPreview(node, previewNode, previewConnections, nodeManifest) {
    const previewSize = (0, container_cjs_namespaceObject.getNodeDimensions)(previewNode);
    const nodeSize = (0, container_cjs_namespaceObject.getNodeDimensions)(node, (0, collapse_cjs_namespaceObject.getExpandedSize)(nodeManifest?.display.shape));
    if (previewSize.width === nodeSize.width && previewSize.height === nodeSize.height) return node;
    const previewHandlePosition = getPrimaryPreviewHandlePosition(previewNode, previewConnections);
    const previewCenterX = previewNode.position.x + previewSize.width / 2;
    const previewCenterY = previewNode.position.y + previewSize.height / 2;
    if (previewHandlePosition === react_cjs_namespaceObject.Position.Left) return {
        ...node,
        position: {
            x: previewNode.position.x,
            y: previewCenterY - nodeSize.height / 2
        }
    };
    if (previewHandlePosition === react_cjs_namespaceObject.Position.Right) return {
        ...node,
        position: {
            x: previewNode.position.x + previewSize.width - nodeSize.width,
            y: previewCenterY - nodeSize.height / 2
        }
    };
    if (previewHandlePosition === react_cjs_namespaceObject.Position.Top) return {
        ...node,
        position: {
            x: previewCenterX - nodeSize.width / 2,
            y: previewNode.position.y
        }
    };
    if (previewHandlePosition === react_cjs_namespaceObject.Position.Bottom) return {
        ...node,
        position: {
            x: previewCenterX - nodeSize.width / 2,
            y: previewNode.position.y + previewSize.height - nodeSize.height
        }
    };
    return {
        ...node,
        position: {
            x: previewCenterX - nodeSize.width / 2,
            y: previewCenterY - nodeSize.height / 2
        }
    };
}
function resolveScopedCollisions(nodes, insertedNode, options) {
    const siblingNodes = nodes.filter((node)=>node.parentId === insertedNode.parentId);
    const resolvedSiblings = (0, index_cjs_namespaceObject.resolveCollisions)(siblingNodes, options);
    const resolvedSiblingById = new Map(resolvedSiblings.map((node)=>[
            node.id,
            node
        ]));
    const resolvedNodes = nodes.map((node)=>resolvedSiblingById.get(node.id) ?? node);
    return {
        nodes: resolvedNodes,
        insertedNode: resolvedSiblingById.get(insertedNode.id)
    };
}
function shiftForEdgeInsertion({ nodes, edges, previewNode, insertedNode, getNodeDimensions }) {
    const originalEdge = getOriginalEdge(previewNode);
    if (!originalEdge) return null;
    const targetNode = nodes.find((node)=>node.id === originalEdge.target);
    if (!targetNode || targetNode.parentId !== insertedNode.parentId) return null;
    const insertedSize = getNodeDimensions(insertedNode);
    let insertedX = insertedNode.position.x;
    const sourceNode = nodes.find((node)=>node.id === originalEdge.source);
    if (sourceNode && sourceNode.parentId === insertedNode.parentId) {
        const sourceSize = getNodeDimensions(sourceNode);
        const requiredInsertedLeft = sourceNode.position.x + sourceSize.width + TOP_LEVEL_INSERTION_GAP_PX;
        if (insertedX < requiredInsertedLeft) insertedX = (0, NodeUtils_cjs_namespaceObject.snapUpToGrid)(requiredInsertedLeft);
    }
    const isBackEdge = void 0 !== sourceNode && sourceNode.parentId === insertedNode.parentId && sourceNode.position.x >= targetNode.position.x;
    const requiredTargetLeft = insertedX + insertedSize.width + TOP_LEVEL_INSERTION_GAP_PX;
    const downstreamShift = !isBackEdge && targetNode.position.x < requiredTargetLeft ? (0, NodeUtils_cjs_namespaceObject.snapUpToGrid)(requiredTargetLeft - targetNode.position.x) : 0;
    const chainIds = downstreamShift > 0 ? new Set((0, container_cjs_namespaceObject.collectLinearDownstreamSiblings)({
        startNodeId: targetNode.id,
        parentId: insertedNode.parentId,
        nodes,
        edges
    })) : new Set();
    chainIds.delete(originalEdge.source);
    const insertedXChanged = insertedX !== insertedNode.position.x;
    if (!insertedXChanged && 0 === chainIds.size) return null;
    const updatedInsertedNode = insertedXChanged ? {
        ...insertedNode,
        position: {
            x: insertedX,
            y: insertedNode.position.y
        }
    } : insertedNode;
    const updatedNodes = nodes.map((node)=>{
        if (node.id === insertedNode.id) return updatedInsertedNode;
        if (chainIds.has(node.id)) return {
            ...node,
            position: {
                x: node.position.x + downstreamShift,
                y: node.position.y
            }
        };
        return node;
    });
    return {
        nodes: updatedNodes,
        insertedNode: updatedInsertedNode
    };
}
function placeAddedNode({ nodes, edges, previewNode, insertedNode, layout, ignoredNodeTypes }) {
    const { getNodeDimensions, isContainerNode, getContainerFitGeometry } = layout;
    const placement = (0, container_cjs_namespaceObject.getContainerPlacement)(previewNode);
    if (placement) {
        const containerNode = nodes.find((node)=>node.id === placement.containerId);
        if (!containerNode) return resolveScopedCollisions(nodes, insertedNode, {
            ignoredNodeTypes,
            getNodeDimensions
        });
        if (!isContainerNode(containerNode)) return resolveScopedCollisions(nodes, insertedNode, {
            ignoredNodeTypes,
            getNodeDimensions
        });
        const placedNodes = (0, container_cjs_namespaceObject.placeContainerNode)({
            nodes,
            insertedNode,
            placement,
            edges,
            getNodeDimensions,
            safeArea: (0, container_cjs_namespaceObject.getContainerSafeArea)(containerNode),
            getContainerFitGeometry,
            ignoredNodeTypes
        });
        const placedInsertedNode = placedNodes.find((node)=>node.id === insertedNode.id);
        const resolved = resolveScopedCollisions(placedNodes, placedInsertedNode, {
            ignoredNodeTypes,
            getNodeDimensions
        });
        if (resolved.insertedNode.parentId) {
            const refitted = (0, container_cjs_namespaceObject.fitContainersAndPushSiblings)({
                nodes: resolved.nodes,
                containerIds: [
                    resolved.insertedNode.parentId
                ],
                getContainerFitGeometry,
                getNodeDimensions,
                ignoredNodeTypes,
                gap: container_cjs_namespaceObject.CONTAINER_SEQUENCE_GAP_PX
            });
            return {
                nodes: refitted,
                insertedNode: refitted.find((node)=>node.id === resolved.insertedNode.id) ?? resolved.insertedNode
            };
        }
        return resolved;
    }
    const shifted = shiftForEdgeInsertion({
        nodes,
        edges,
        previewNode,
        insertedNode,
        getNodeDimensions
    });
    const placementResult = resolveScopedCollisions(shifted?.nodes ?? nodes, shifted?.insertedNode ?? insertedNode, {
        ignoredNodeTypes,
        getNodeDimensions
    });
    if (!placementResult.insertedNode.parentId) return placementResult;
    const fittedNodes = (0, container_cjs_namespaceObject.fitContainersAndPushSiblings)({
        nodes: placementResult.nodes,
        containerIds: [
            placementResult.insertedNode.parentId
        ],
        getContainerFitGeometry,
        getNodeDimensions,
        ignoredNodeTypes,
        gap: container_cjs_namespaceObject.CONTAINER_SEQUENCE_GAP_PX
    });
    return {
        nodes: fittedNodes,
        insertedNode: fittedNodes.find((node)=>node.id === placementResult.insertedNode.id) ?? placementResult.insertedNode
    };
}
exports.alignNodeToPreview = __webpack_exports__.alignNodeToPreview;
exports.getOriginalEdge = __webpack_exports__.getOriginalEdge;
exports.placeAddedNode = __webpack_exports__.placeAddedNode;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "alignNodeToPreview",
    "getOriginalEdge",
    "placeAddedNode"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
