"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OutboundDependencies = void 0;
const tslib_1 = require("tslib");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const React = tslib_1.__importStar(require("react"));
const constants_1 = require("../../constants");
const useComputeVisGraph_1 = require("../../hooks/useComputeVisGraph");
const Graph_1 = require("./Graph");
const NodeDialog_1 = require("./NodeDialog");
exports.OutboundDependencies = ({ className, node, edges, getNetwork }) => {
    const rootNodeId = node.id;
    const visGraph = useComputeVisGraph_1.useComputeVisGraph(node, edges);
    const visNetwork = React.useRef();
    const [activeNodeId, setActiveNodeId] = React.useState();
    const [activeNodeEdge, setActiveNodeEdge] = React.useState();
    const onClickNode = React.useCallback((e) => {
        const nodeId = e.nodes[0];
        if (!nodeId)
            return;
        const foundEdge = edges.find(edge => edge.toBranchNodeId === nodeId || edge.fromBranchNodeId === nodeId);
        if (foundEdge) {
            setActiveNodeId(nodeId);
            setActiveNodeEdge(foundEdge);
        }
    }, [edges]);
    React.useEffect(() => {
        setActiveNodeEdge(undefined);
    }, [rootNodeId]);
    React.useEffect(() => {
        if (visNetwork.current) {
            visNetwork.current.setSelection({ edges: [], nodes: [activeNodeId || node.id] });
        }
    }, [activeNodeId, node.id]);
    if (!visGraph || !visGraph.nodes.length) {
        return React.createElement("div", null,
            "This ",
            constants_1.NodeTypePrettyName[node.type],
            " does not have any outbound dependencies.");
    }
    return (React.createElement("div", { className: classnames_1.default(className, 'w-full h-full') },
        React.createElement(Graph_1.Graph, { id: rootNodeId, graph: visGraph, events: {
                click: onClickNode,
            }, getNetwork: network => {
                visNetwork.current = network;
                if (network) {
                    network.setSelection({ edges: [], nodes: [activeNodeId || node.id] });
                    getNetwork === null || getNetwork === void 0 ? void 0 : getNetwork(network);
                }
            } }),
        React.createElement(NodeDialog_1.NodeDialog, { direction: activeNodeId === (activeNodeEdge === null || activeNodeEdge === void 0 ? void 0 : activeNodeEdge.toBranchNodeId) ? 'to' : 'from', edge: activeNodeEdge, onClose: () => setActiveNodeEdge(undefined) })));
};
//# sourceMappingURL=Outbound.js.map