"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const logger_1 = require("@pnpm/logger");
const dp = require("dependency-path");
const R = require("ramda");
const filterImporter_1 = require("./filterImporter");
const filterShrinkwrap_1 = require("./filterShrinkwrap");
const logger = logger_1.default('shrinkwrap');
function filterByImporters(shr, importerIds, opts) {
    if (R.equals(importerIds.sort(), R.keys(shr.importers).sort())) {
        return filterShrinkwrap_1.default(shr, opts);
    }
    const importerDeps = importerIds
        .map((importerId) => shr.importers[importerId])
        .map((importer) => (Object.assign({}, (opts.include.dependencies && importer.dependencies || {}), (opts.include.devDependencies && importer.devDependencies || {}), (opts.include.optionalDependencies && importer.optionalDependencies || {}))))
        .map(R.toPairs);
    const directDepPaths = R.unnest(importerDeps)
        .map(([pkgName, ref]) => dp.refToRelative(ref, pkgName))
        .filter((nodeId) => nodeId !== null);
    const packages = shr.packages &&
        pickPkgsWithAllDeps(shr.packages, directDepPaths, {
            failOnMissingDependencies: opts.failOnMissingDependencies,
            include: opts.include,
            skipped: opts.skipped,
        }) || {};
    const importers = importerIds.reduce((acc, importerId) => {
        acc[importerId] = filterImporter_1.default(shr.importers[importerId], opts.include);
        return acc;
    }, Object.assign({}, shr.importers));
    return {
        importers,
        packages,
        shrinkwrapVersion: shr.shrinkwrapVersion,
    };
}
exports.default = filterByImporters;
function pickPkgsWithAllDeps(pkgSnapshots, relDepPaths, opts) {
    const pickedPackages = {};
    pkgAllDeps(pkgSnapshots, pickedPackages, relDepPaths, opts);
    return pickedPackages;
}
function pkgAllDeps(pkgSnapshots, pickedPackages, relDepPaths, opts) {
    for (const relDepPath of relDepPaths) {
        if (pickedPackages[relDepPath] || opts.skipped.has(relDepPath))
            continue;
        const pkgSnapshot = pkgSnapshots[relDepPath];
        if (!pkgSnapshot && !relDepPath.startsWith('link:')) {
            const message = `No entry for "${relDepPath}" in shrinkwrap.yaml`;
            if (opts.failOnMissingDependencies) {
                const err = new Error(message);
                err['code'] = 'ERR_PNPM_SHRINKWRAP_MISSING_DEPENDENCY'; // tslint:disable-line:no-string-literal
                throw err;
            }
            logger.debug(message);
            continue;
        }
        pickedPackages[relDepPath] = pkgSnapshot;
        const nextRelDepPaths = R.toPairs(Object.assign({}, pkgSnapshot.dependencies, (opts.include.optionalDependencies && pkgSnapshot.optionalDependencies || {})))
            .map(([pkgName, ref]) => dp.refToRelative(ref, pkgName))
            .filter((nodeId) => nodeId !== null);
        pkgAllDeps(pkgSnapshots, pickedPackages, nextRelDepPaths, opts);
    }
}
//# sourceMappingURL=filterByImporters.js.map