"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getFileContents = exports.getPnpmChildNode = void 0;
const path = require("path");
const fs = require("fs");
const __1 = require("../..");
const util_1 = require("../util");
const error_catalog_nodejs_public_1 = require("@snyk/error-catalog-nodejs-public");
const out_of_sync_error_1 = require("../../errors/out-of-sync-error");
const debugModule = require("debug");
const constants_1 = require("./constants");
const debug = debugModule('snyk-pnpm-workspaces');
const getPnpmChildNode = (name, depInfo, pkgs, strictOutOfSync, includeOptionalDeps, includeDevDeps, lockfileParser) => {
    const resolvedVersion = lockfileParser.excludeTransPeerDepsVersions(depInfo.version);
    let childNodeKey = `${name}@${resolvedVersion}`;
    // For aliases, the version is the dependency path that
    // shows up in the packages section of lockfiles
    if (lockfileParser.resolvedPackages[resolvedVersion]) {
        childNodeKey = lockfileParser.resolvedPackages[resolvedVersion];
    }
    if (lockfileParser.resolvedPackages[childNodeKey]) {
        childNodeKey = lockfileParser.resolvedPackages[childNodeKey];
    }
    if (!pkgs[childNodeKey]) {
        if (strictOutOfSync && !/^file:/.test(depInfo.version)) {
            const errMessage = `Dependency ${childNodeKey} was not found in ` +
                `${out_of_sync_error_1.LOCK_FILE_NAME[__1.LockfileType.pnpm]}. Your package.json and ` +
                `${out_of_sync_error_1.LOCK_FILE_NAME[__1.LockfileType.pnpm]} are probably out of sync. Please run ` +
                `"${out_of_sync_error_1.INSTALL_COMMAND[__1.LockfileType.pnpm]}" and try again.`;
            debug(errMessage);
            throw new error_catalog_nodejs_public_1.OpenSourceEcosystems.PnpmOutOfSyncError(errMessage);
        }
        else {
            return {
                id: childNodeKey,
                name: name,
                version: resolvedVersion || constants_1.UNDEFINED_VERSION,
                dependencies: {},
                isDev: depInfo.isDev,
                missingLockFileEntry: true,
            };
        }
    }
    else {
        const depData = pkgs[childNodeKey];
        const dependencies = (0, util_1.getGraphDependencies)(depData.dependencies || {}, depInfo.isDev);
        const devDependencies = includeDevDeps
            ? (0, util_1.getGraphDependencies)(depData.devDependencies || {}, true)
            : {};
        const optionalDependencies = includeOptionalDeps
            ? (0, util_1.getGraphDependencies)(depData.optionalDependencies || {}, depInfo.isDev)
            : {};
        return {
            id: `${depData.name}@${depData.version}`,
            name: depData.name,
            version: depData.version || constants_1.UNDEFINED_VERSION,
            dependencies: Object.assign(Object.assign(Object.assign({}, dependencies), optionalDependencies), devDependencies),
            isDev: depInfo.isDev,
        };
    }
};
exports.getPnpmChildNode = getPnpmChildNode;
function getFileContents(root, fileName) {
    const fullPath = path.resolve(root, fileName);
    if (!fs.existsSync(fullPath)) {
        throw new Error('Manifest ' + fileName + ' not found at location: ' + fileName);
    }
    const content = fs.readFileSync(fullPath, 'utf-8');
    return {
        content,
        fileName,
    };
}
exports.getFileContents = getFileContents;
//# sourceMappingURL=utils.js.map