"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildDepGraphYarnLockV1Workspace = void 0;
const dep_graph_1 = require("@snyk/dep-graph");
const util_1 = require("../util");
const util_2 = require("./util");
const event_loop_spinner_1 = require("event-loop-spinner");
const buildDepGraphYarnLockV1Workspace = async (extractedYarnLockV1Pkgs, pkgJson, workspacePkgNameToVersion, options) => {
    const { includeDevDeps, strictOutOfSync, includeOptionalDeps } = options;
    const depGraphBuilder = new dep_graph_1.DepGraphBuilder({ name: 'yarn' }, { name: pkgJson.name, version: pkgJson.version });
    const visitedMap = new Set();
    const topLevelDeps = (0, util_1.getTopLevelDeps)(pkgJson, { includeDevDeps });
    const rootNode = {
        id: 'root-node',
        name: pkgJson.name,
        version: pkgJson.version,
        dependencies: topLevelDeps,
        isDev: false,
    };
    await dfsVisit(depGraphBuilder, rootNode, visitedMap, extractedYarnLockV1Pkgs, workspacePkgNameToVersion, strictOutOfSync, includeOptionalDeps);
    return depGraphBuilder.build();
};
exports.buildDepGraphYarnLockV1Workspace = buildDepGraphYarnLockV1Workspace;
/**
 * Use DFS to add all nodes and edges to the depGraphBuilder and prune cyclic nodes.
 * The colorMap keep track of the state of node during traversal.
 *  - If a node doesn't exist in the map, it means it hasn't been visited.
 *  - If a node is GRAY, it means it has already been discovered but its subtree hasn't been fully traversed.
 *  - If a node is BLACK, it means its subtree has already been fully traversed.
 *  - When first exploring an edge, if it points to a GRAY node, a cycle is found and the GRAY node is pruned.
 *     - A pruned node has id `${originalId}|1`
 * When coming across another workspace package as child node, simply add the node and edge to the graph and mark it as BLACK.
 */
const dfsVisit = async (depGraphBuilder, node, visitedMap, extractedYarnLockV1Pkgs, workspacePkgNameToVersion, strictOutOfSync, includeOptionalDeps) => {
    visitedMap.add(node.id);
    for (const [name, depInfo] of Object.entries(node.dependencies || {})) {
        if (event_loop_spinner_1.eventLoopSpinner.isStarving()) {
            await event_loop_spinner_1.eventLoopSpinner.spin();
        }
        const isWorkspacePkg = !!workspacePkgNameToVersion[name];
        const childNode = (0, util_2.getChildNodeYarnLockV1Workspace)(name, depInfo, workspacePkgNameToVersion, extractedYarnLockV1Pkgs, strictOutOfSync, includeOptionalDeps);
        if (!visitedMap.has(childNode.id)) {
            (0, util_1.addPkgNodeToGraph)(depGraphBuilder, childNode, {
                isCyclic: false,
                isWorkspacePkg,
            });
            if (!isWorkspacePkg) {
                await dfsVisit(depGraphBuilder, childNode, visitedMap, extractedYarnLockV1Pkgs, workspacePkgNameToVersion, strictOutOfSync, includeOptionalDeps);
            }
        }
        depGraphBuilder.connectDep(node.id, childNode.id);
    }
};
//# sourceMappingURL=build-depgraph-workspace-package.js.map