"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__, {
    PREVIEW_EDGE_STYLE: ()=>PREVIEW_EDGE_STYLE,
    isPreviewEdge: ()=>isPreviewEdge,
    removePreviewFromReactFlow: ()=>removePreviewFromReactFlow,
    applyPreviewToReactFlow: ()=>applyPreviewToReactFlow,
    getOppositePosition: ()=>getOppositePosition,
    createPreviewNode: ()=>createPreviewNode
});
const react_cjs_namespaceObject = require("../xyflow/react.cjs");
const external_constants_cjs_namespaceObject = require("../constants.cjs");
const external_container_cjs_namespaceObject = require("./container.cjs");
const external_NodeUtils_cjs_namespaceObject = require("./NodeUtils.cjs");
const PREVIEW_EDGE_STYLE = {
    strokeDasharray: '5,5',
    opacity: 0.8,
    stroke: 'var(--canvas-selection-indicator)',
    strokeWidth: 2
};
function isPreviewEdge(edge) {
    return edge.id === external_constants_cjs_namespaceObject.PREVIEW_EDGE_ID || edge.source === external_constants_cjs_namespaceObject.PREVIEW_NODE_ID || edge.target === external_constants_cjs_namespaceObject.PREVIEW_NODE_ID;
}
function getOppositePosition(position) {
    switch(position){
        case react_cjs_namespaceObject.Position.Left:
            return react_cjs_namespaceObject.Position.Right;
        case react_cjs_namespaceObject.Position.Right:
            return react_cjs_namespaceObject.Position.Left;
        case react_cjs_namespaceObject.Position.Top:
            return react_cjs_namespaceObject.Position.Bottom;
        case react_cjs_namespaceObject.Position.Bottom:
            return react_cjs_namespaceObject.Position.Top;
        default:
            return react_cjs_namespaceObject.Position.Right;
    }
}
function calculatePositionFromDrop(dropPosition, handlePosition, previewNodeSize) {
    switch(handlePosition){
        case react_cjs_namespaceObject.Position.Left:
            return {
                x: dropPosition.x - previewNodeSize.width,
                y: dropPosition.y - previewNodeSize.height / 2
            };
        case react_cjs_namespaceObject.Position.Right:
            return {
                x: dropPosition.x,
                y: dropPosition.y - previewNodeSize.height / 2
            };
        case react_cjs_namespaceObject.Position.Top:
            return {
                x: dropPosition.x - previewNodeSize.width / 2,
                y: dropPosition.y - previewNodeSize.height
            };
        case react_cjs_namespaceObject.Position.Bottom:
            return {
                x: dropPosition.x - previewNodeSize.width / 2,
                y: dropPosition.y
            };
        default:
            return {
                x: dropPosition.x,
                y: dropPosition.y - previewNodeSize.height / 2
            };
    }
}
function calculateCenteredPosition(centerPosition, previewNodeSize) {
    return {
        x: centerPosition.x - previewNodeSize.width / 2,
        y: centerPosition.y - previewNodeSize.height / 2
    };
}
function computeSpreadOffset(handle, size) {
    const index = handle?.index ?? null;
    const count = handle?.count ?? 1;
    if (null === index || count <= 1) return 0;
    if (index < Math.floor(count / 2)) return -size;
    if (index >= Math.ceil(count / 2)) return size;
    return 0;
}
function calculateAutoPosition(sourceNode, handlePosition, previewNodeSize, existingNodes, offset = 5 * external_constants_cjs_namespaceObject.GRID_SPACING, ignoredNodeTypes = [], handle) {
    const sourceAbsolutePosition = sourceNode.parentId ? (0, external_NodeUtils_cjs_namespaceObject.getAbsolutePosition)(sourceNode, existingNodes) : sourceNode.position;
    const sourceSize = (0, external_container_cjs_namespaceObject.getNodeDimensions)(sourceNode);
    const anchorX = handle?.anchor.x ?? sourceAbsolutePosition.x + sourceSize.width / 2;
    const anchorY = handle?.anchor.y ?? sourceAbsolutePosition.y + sourceSize.height / 2;
    const ignoredNodeIds = new Set();
    let parentId = sourceNode.parentId;
    while(parentId){
        ignoredNodeIds.add(parentId);
        parentId = existingNodes.find((node)=>node.id === parentId)?.parentId;
    }
    const nodesWithAbsolutePositions = existingNodes.filter((node)=>node.id !== external_constants_cjs_namespaceObject.PREVIEW_NODE_ID && !ignoredNodeIds.has(node.id)).map((node)=>({
            ...node,
            position: node.parentId ? (0, external_NodeUtils_cjs_namespaceObject.getAbsolutePosition)(node, existingNodes) : node.position
        }));
    let initialPosition;
    let direction;
    switch(handlePosition){
        case react_cjs_namespaceObject.Position.Left:
            initialPosition = {
                x: sourceAbsolutePosition.x - previewNodeSize.width - offset,
                y: anchorY - previewNodeSize.height / 2
            };
            direction = 'left';
            break;
        case react_cjs_namespaceObject.Position.Right:
            initialPosition = {
                x: sourceAbsolutePosition.x + sourceSize.width + offset,
                y: anchorY - previewNodeSize.height / 2 + computeSpreadOffset(handle, previewNodeSize.height)
            };
            direction = 'right';
            break;
        case react_cjs_namespaceObject.Position.Top:
            initialPosition = {
                x: anchorX - previewNodeSize.width / 2 + computeSpreadOffset(handle, previewNodeSize.width / 2),
                y: sourceAbsolutePosition.y - previewNodeSize.height - offset
            };
            direction = 'top';
            break;
        case react_cjs_namespaceObject.Position.Bottom:
            initialPosition = {
                x: anchorX - previewNodeSize.width / 2 + computeSpreadOffset(handle, previewNodeSize.width / 2),
                y: sourceAbsolutePosition.y + sourceSize.height + offset
            };
            direction = 'bottom';
            break;
        default:
            initialPosition = {
                x: sourceAbsolutePosition.x + sourceSize.width + offset,
                y: anchorY - previewNodeSize.height / 2
            };
            direction = 'right';
    }
    const overflowDirection = {
        x: anchorX >= sourceAbsolutePosition.x + sourceSize.width / 2 ? 'right' : 'left',
        y: anchorY >= sourceAbsolutePosition.y + sourceSize.height / 2 ? 'down' : 'up'
    };
    return (0, external_NodeUtils_cjs_namespaceObject.getNonOverlappingPositionForDirection)(nodesWithAbsolutePositions, initialPosition, previewNodeSize, direction, offset, ignoredNodeTypes, overflowDirection);
}
function createPreviewNode(options) {
    const { sourceNodeId, sourceHandleId, reactFlowInstance, position, data, sourceHandleType = 'source', previewNodeSize = {
        width: external_constants_cjs_namespaceObject.DEFAULT_NODE_SIZE,
        height: external_constants_cjs_namespaceObject.DEFAULT_NODE_SIZE
    }, handlePosition = react_cjs_namespaceObject.Position.Right, ignoredNodeTypes = [], positionMode = 'drop', sourceBoundaryOf } = options;
    const sourceNode = reactFlowInstance.getNode(sourceNodeId);
    if (!sourceNode) {
        console.warn(`Source node ${sourceNodeId} not found`);
        return null;
    }
    const treatPreviewAsSource = 'target' === sourceHandleType;
    const existingNodes = reactFlowInstance.getNodes().filter((n)=>n.id !== external_constants_cjs_namespaceObject.PREVIEW_NODE_ID);
    const internalNode = void 0 === position ? reactFlowInstance.getInternalNode(sourceNodeId) : void 0;
    const handle = internalNode ? (0, external_NodeUtils_cjs_namespaceObject.resolveHandleContext)(internalNode, sourceHandleId, handlePosition, {
        boundaryOf: sourceBoundaryOf
    }) : void 0;
    const nodePosition = position ? 'center' === positionMode ? calculateCenteredPosition(position, previewNodeSize) : calculatePositionFromDrop(position, handlePosition, previewNodeSize) : calculateAutoPosition(sourceNode, handlePosition, previewNodeSize, existingNodes, void 0, ignoredNodeTypes, handle);
    const handleFacingSource = getOppositePosition(handlePosition);
    const finalData = {
        ...data ?? {}
    };
    finalData.inputHandlePosition = treatPreviewAsSource ? handlePosition : handleFacingSource;
    finalData.outputHandlePosition = treatPreviewAsSource ? handleFacingSource : handlePosition;
    const previewNode = {
        id: external_constants_cjs_namespaceObject.PREVIEW_NODE_ID,
        type: 'preview',
        position: nodePosition,
        ...previewNodeSize,
        selected: true,
        data: finalData
    };
    const previewSourceAndTargetData = treatPreviewAsSource ? {
        source: external_constants_cjs_namespaceObject.PREVIEW_NODE_ID,
        sourceHandle: 'output',
        target: sourceNodeId,
        targetHandle: sourceHandleId
    } : {
        source: sourceNodeId,
        sourceHandle: sourceHandleId,
        target: external_constants_cjs_namespaceObject.PREVIEW_NODE_ID,
        targetHandle: 'input'
    };
    const previewEdge = {
        id: external_constants_cjs_namespaceObject.PREVIEW_EDGE_ID,
        ...previewSourceAndTargetData,
        type: 'default',
        style: PREVIEW_EDGE_STYLE
    };
    return {
        node: previewNode,
        edge: previewEdge
    };
}
function applyPreviewToReactFlow(preview, reactFlowInstance) {
    setTimeout(()=>{
        reactFlowInstance.setNodes((nodes)=>[
                ...nodes.filter((n)=>n.id !== external_constants_cjs_namespaceObject.PREVIEW_NODE_ID).map((n)=>({
                        ...n,
                        selected: false
                    })),
                preview.node
            ]);
        reactFlowInstance.setEdges((edges)=>[
                ...edges.filter((edge)=>!isPreviewEdge(edge)),
                preview.edge
            ]);
    }, 0);
}
function removePreviewFromReactFlow(reactFlowInstance) {
    reactFlowInstance.setNodes((nodes)=>nodes.filter((n)=>n.id !== external_constants_cjs_namespaceObject.PREVIEW_NODE_ID));
    reactFlowInstance.setEdges((edges)=>edges.filter((edge)=>!isPreviewEdge(edge)));
}
exports.PREVIEW_EDGE_STYLE = __webpack_exports__.PREVIEW_EDGE_STYLE;
exports.applyPreviewToReactFlow = __webpack_exports__.applyPreviewToReactFlow;
exports.createPreviewNode = __webpack_exports__.createPreviewNode;
exports.getOppositePosition = __webpack_exports__.getOppositePosition;
exports.isPreviewEdge = __webpack_exports__.isPreviewEdge;
exports.removePreviewFromReactFlow = __webpack_exports__.removePreviewFromReactFlow;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "PREVIEW_EDGE_STYLE",
    "applyPreviewToReactFlow",
    "createPreviewNode",
    "getOppositePosition",
    "isPreviewEdge",
    "removePreviewFromReactFlow"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
