"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const logger_1 = require("@pnpm/logger");
const isCI = require("is-ci");
const pnpm_shrinkwrap_1 = require("pnpm-shrinkwrap");
const R = require("ramda");
const constants_1 = require("../constants");
function default_1(opts) {
    return __awaiter(this, void 0, void 0, function* () {
        let shrinkwrapVersion;
        if (opts.forceSharedShrinkwrap || opts.importers.length > 1 || opts.importers[0] && opts.importers[0].prefix !== opts.shrinkwrapDirectory) {
            shrinkwrapVersion = constants_1.SHRINKWRAP_NEXT_VERSION;
        }
        else {
            shrinkwrapVersion = constants_1.SHRINKWRAP_VERSION;
        }
        // ignore `shrinkwrap.yaml` on CI servers
        // a latest pnpm should not break all the builds
        const shrOpts = {
            ignoreIncompatible: opts.force || isCI,
            wantedVersion: shrinkwrapVersion,
        };
        const files = yield Promise.all([
            opts.shrinkwrap && pnpm_shrinkwrap_1.readWanted(opts.shrinkwrapDirectory, shrOpts)
                || (yield pnpm_shrinkwrap_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,
                    }),
            pnpm_shrinkwrap_1.readCurrent(opts.shrinkwrapDirectory, shrOpts),
        ]);
        const sopts = { shrinkwrapVersion };
        const importerIds = opts.importers.map((importer) => importer.id);
        const currentShrinkwrap = files[1] || pnpm_shrinkwrap_1.create(opts.registry, importerIds, sopts);
        for (const importerId of importerIds) {
            if (!currentShrinkwrap.importers[importerId]) {
                currentShrinkwrap.importers[importerId] = {
                    specifiers: {},
                };
            }
        }
        const wantedShrinkwrap = files[0] ||
            currentShrinkwrap && R.clone(currentShrinkwrap) ||
            pnpm_shrinkwrap_1.create(opts.registry, 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