"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.computeVisGraph = exports.useComputeVisGraph = void 0;
const tslib_1 = require("tslib");
const lodash_1 = require("lodash");
const React = tslib_1.__importStar(require("react"));
const constants_1 = require("../constants");
const node_1 = require("../utils/node");
function useComputeVisGraph(rootNode, edges) {
    return React.useMemo(() => computeVisGraph(rootNode, edges), [rootNode, edges]);
}
exports.useComputeVisGraph = useComputeVisGraph;
function computeVisGraph(rootNode, edges) {
    const visGraph = {
        nodes: [
            {
                level: 0,
                id: rootNode.id,
                title: rootNode.uri,
                label: rootNode.name,
                icon: {
                    code: constants_1.NodeTypeIconsUnicode[rootNode.type],
                    color: '#ef932b',
                },
            },
        ],
        edges: [],
    };
    const nodesInGraph = [rootNode.id];
    for (const edge of edges) {
        if (!nodesInGraph.includes(edge.fromBranchNodeId) && !node_1.isIrrelevantNodeType(edge.fromBranchNodeType)) {
            nodesInGraph.push(edge.fromBranchNodeId);
            visGraph.nodes.push({
                level: edge.depth,
                id: edge.fromBranchNodeId,
                label: edge.fromBranchNodeName,
                title: edge.fromBranchNodeUri,
                icon: {
                    code: constants_1.NodeTypeIconsUnicode[edge.fromBranchNodeType],
                },
            });
        }
        if (!nodesInGraph.includes(edge.toBranchNodeId) && !node_1.isIrrelevantNodeType(edge.toBranchNodeType)) {
            nodesInGraph.push(edge.toBranchNodeId);
            visGraph.nodes.push({
                level: edge.depth,
                id: edge.toBranchNodeId,
                label: edge.toBranchNodeName,
                title: edge.toBranchNodeUri,
                icon: {
                    code: constants_1.NodeTypeIconsUnicode[edge.toBranchNodeType],
                },
            });
        }
        visGraph.edges.push({
            id: edge.id,
            from: edge.fromBranchNodeId,
            to: edge.toBranchNodeId,
            title: edge.fromBranchNodePath,
            color: '#c3cdd4',
        });
    }
    visGraph.nodes = lodash_1.sortBy(visGraph.nodes, 'label');
    return visGraph;
}
exports.computeVisGraph = computeVisGraph;
//# sourceMappingURL=useComputeVisGraph.js.map