"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const constants_1 = require("@pnpm/constants");
const error_1 = require("@pnpm/error");
const logger_1 = require("@pnpm/logger");
const dp = require("dependency-path");
const R = require("ramda");
const filterImporter_1 = require("./filterImporter");
const filterLockfile_1 = require("./filterLockfile");
const logger = logger_1.default('lockfile');
function filterByImporters(lockfile, importerIds, opts) {
    if (R.equals(importerIds.sort(), R.keys(lockfile.importers).sort())) {
        return filterLockfile_1.default(lockfile, opts);
    }
    const importerDeps = importerIds
        .map((importerId) => lockfile.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 = lockfile.packages &&
        pickPkgsWithAllDeps(lockfile.packages, directDepPaths, {
            failOnMissingDependencies: opts.failOnMissingDependencies,
            include: opts.include,
            skipped: opts.skipped,
        }) || {};
    const importers = importerIds.reduce((acc, importerId) => {
        acc[importerId] = filterImporter_1.default(lockfile.importers[importerId], opts.include);
        return acc;
    }, Object.assign({}, lockfile.importers));
    return {
        importers,
        lockfileVersion: lockfile.lockfileVersion,
        packages,
    };
}
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 ${constants_1.WANTED_LOCKFILE}`;
            if (opts.failOnMissingDependencies) {
                throw new error_1.default('LOCKFILE_MISSING_DEPENDENCY', message);
            }
            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);
    }
}
