"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__, {
    usePreviewNode: ()=>usePreviewNode
});
const react_cjs_namespaceObject = require("../xyflow/react.cjs");
const external_react_namespaceObject = require("react");
const shallow_namespaceObject = require("zustand/shallow");
const external_constants_cjs_namespaceObject = require("../constants.cjs");
const index_cjs_namespaceObject = require("../core/index.cjs");
const createPreviewNode_cjs_namespaceObject = require("../utils/createPreviewNode.cjs");
const previewNodeSelectedSelector = (state)=>{
    const node = state.nodes.find((n)=>n.id === external_constants_cjs_namespaceObject.PREVIEW_NODE_ID);
    return node?.selected ?? false;
};
const edgesConnectedToPreviewSelector = (state)=>state.edges.filter(createPreviewNode_cjs_namespaceObject.isPreviewEdge).map((edge)=>({
            id: edge.id,
            source: edge.source,
            target: edge.target,
            sourceHandle: edge.sourceHandle,
            targetHandle: edge.targetHandle
        }));
const usePreviewNode = ()=>{
    const reactFlowInstance = (0, react_cjs_namespaceObject.useReactFlow)();
    const isPreviewNodeSelected = (0, react_cjs_namespaceObject.useStore)(previewNodeSelectedSelector);
    const previewEdges = (0, react_cjs_namespaceObject.useStore)(edgesConnectedToPreviewSelector, shallow_namespaceObject.shallow);
    const registry = (0, index_cjs_namespaceObject.useOptionalNodeTypeRegistry)();
    const previewNode = isPreviewNodeSelected ? reactFlowInstance.getNode(external_constants_cjs_namespaceObject.PREVIEW_NODE_ID) ?? null : null;
    const connectionInfo = (0, external_react_namespaceObject.useMemo)(()=>{
        if (!isPreviewNodeSelected) return null;
        const connections = previewEdges.map((previewEdge)=>{
            const sourceIsPreviewNode = previewEdge.source === external_constants_cjs_namespaceObject.PREVIEW_NODE_ID;
            const existingNodeId = sourceIsPreviewNode ? previewEdge.target : previewEdge.source;
            const existingNodeType = reactFlowInstance.getNode(existingNodeId)?.type;
            const existingNodeManifest = existingNodeType ? registry?.getManifest(existingNodeType) : void 0;
            const existingHandleId = sourceIsPreviewNode ? previewEdge.targetHandle || 'input' : previewEdge.sourceHandle || 'output';
            const existingHandleManifest = existingNodeManifest?.handleConfiguration.flatMap((hg)=>hg.handles).find((h)=>{
                if (h.id === existingHandleId) return true;
                const repeatHandleIdBase = h.repeat && h.id.split('{')[0];
                if (repeatHandleIdBase) return existingHandleId.startsWith(repeatHandleIdBase);
                return false;
            });
            return {
                addNewNodeAsSource: sourceIsPreviewNode,
                existingNodeId,
                existingHandleId,
                existingNodeManifest,
                existingHandleManifest,
                previewEdgeId: previewEdge.id
            };
        });
        return connections;
    }, [
        isPreviewNodeSelected,
        previewEdges,
        reactFlowInstance,
        registry
    ]);
    return {
        previewNode,
        previewNodeConnectionInfo: connectionInfo
    };
};
exports.usePreviewNode = __webpack_exports__.usePreviewNode;
for(var __rspack_i in __webpack_exports__)if (-1 === [
    "usePreviewNode"
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
