"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const cafs_1 = require("@pnpm/cafs");
const get_context_1 = require("@pnpm/get-context");
const lockfile_utils_1 = require("@pnpm/lockfile-utils");
const logger_1 = require("@pnpm/logger");
const pkgid_to_filename_1 = require("@pnpm/pkgid-to-filename");
const dp = require("dependency-path");
const dint = require("dint");
const loadJsonFile = require("load-json-file");
const pFilter = require("p-filter");
const path = require("path");
const extendStoreStatusOptions_1 = require("./extendStoreStatusOptions");
async function default_1(maybeOpts) {
    const reporter = maybeOpts === null || maybeOpts === void 0 ? void 0 : maybeOpts.reporter;
    if (reporter) {
        logger_1.streamParser.on('data', reporter);
    }
    const opts = await extendStoreStatusOptions_1.default(maybeOpts);
    const { registries, storeDir, skipped, virtualStoreDir, wantedLockfile, } = await get_context_1.getContextForSingleImporter({}, {
        ...opts,
        extraBinPaths: [],
    });
    if (!wantedLockfile)
        return [];
    const pkgs = Object.keys(wantedLockfile.packages || {})
        .filter((depPath) => !skipped.has(depPath))
        .map((depPath) => {
        const pkg = wantedLockfile.packages[depPath];
        return {
            depPath,
            integrity: pkg.resolution['integrity'],
            pkgPath: dp.resolve(registries, depPath),
            ...lockfile_utils_1.nameVerFromPkgSnapshot(depPath, pkg),
        };
    });
    const cafsDir = path.join(storeDir, 'files');
    const modified = await pFilter(pkgs, async ({ integrity, pkgPath, depPath, name }) => {
        const pkgIndexFilePath = integrity
            ? cafs_1.getFilePathInCafs(cafsDir, integrity, 'index')
            : path.join(storeDir, pkgPath, 'integrity.json');
        const { files } = await loadJsonFile(pkgIndexFilePath);
        return (await dint.check(path.join(virtualStoreDir, pkgid_to_filename_1.default(depPath, opts.dir), 'node_modules', name), files)) === false;
    });
    if (reporter) {
        logger_1.streamParser.removeListener('data', reporter);
    }
    return modified.map(({ pkgPath }) => pkgPath);
}
exports.default = default_1;
//# sourceMappingURL=index.js.map