"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const logger_1 = require("@pnpm/logger");
const shrinkwrap_file_1 = require("@pnpm/shrinkwrap-file");
const isCI = require("is-ci");
const R = require("ramda");
const constants_1 = require("../constants");
async function default_1(opts) {
    // ignore `shrinkwrap.yaml` on CI servers
    // a latest pnpm should not break all the builds
    const shrOpts = {
        ignoreIncompatible: opts.force || isCI,
        wantedVersion: constants_1.SHRINKWRAP_VERSION,
    };
    const files = await Promise.all([
        opts.shrinkwrap && shrinkwrap_file_1.readWanted(opts.shrinkwrapDirectory, shrOpts)
            || await shrinkwrap_file_1.existsWanted(opts.shrinkwrapDirectory) &&
                logger_1.default.warn({
                    message: 'A shrinkwrap.yaml file exists. The current configuration prohibits to read or write a shrinkwrap file',
                    prefix: opts.shrinkwrapDirectory,
                }),
        shrinkwrap_file_1.readCurrent(opts.shrinkwrapDirectory, shrOpts),
    ]);
    const sopts = { shrinkwrapVersion: constants_1.SHRINKWRAP_VERSION };
    const importerIds = opts.importers.map((importer) => importer.id);
    const currentShrinkwrap = files[1] || shrinkwrap_file_1.create(importerIds, sopts);
    for (const importerId of importerIds) {
        if (!currentShrinkwrap.importers[importerId]) {
            currentShrinkwrap.importers[importerId] = {
                specifiers: {},
            };
        }
    }
    const wantedShrinkwrap = files[0] ||
        currentShrinkwrap && R.clone(currentShrinkwrap) ||
        shrinkwrap_file_1.create(importerIds, sopts);
    for (const importerId of importerIds) {
        if (!wantedShrinkwrap.importers[importerId]) {
            wantedShrinkwrap.importers[importerId] = {
                specifiers: {},
            };
        }
    }
    return {
        currentShrinkwrap,
        existsCurrentShrinkwrap: !!files[1],
        existsWantedShrinkwrap: !!files[0],
        wantedShrinkwrap,
    };
}
exports.default = default_1;
//# sourceMappingURL=readShrinkwrapFiles.js.map