"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const constants_1 = require("@pnpm/constants");
const lockfile_file_1 = require("@pnpm/lockfile-file");
const logger_1 = __importDefault(require("@pnpm/logger"));
const isCI = require("is-ci");
const R = require("ramda");
async function default_1(opts) {
    var _a, _b, _c;
    // ignore `pnpm-lock.yaml` on CI servers
    // a latest pnpm should not break all the builds
    const lockfileOpts = {
        ignoreIncompatible: opts.force || isCI,
        wantedVersion: constants_1.LOCKFILE_VERSION,
    };
    const fileReads = [];
    let lockfileHadConflicts = false;
    if (opts.useLockfile) {
        if (opts.autofixMergeConflicts) {
            fileReads.push(lockfile_file_1.readWantedLockfileAndAutofixConflicts(opts.lockfileDir, lockfileOpts)
                .then(({ lockfile, hadConflicts }) => {
                lockfileHadConflicts = hadConflicts;
                return lockfile;
            }));
        }
        else {
            fileReads.push(lockfile_file_1.readWantedLockfile(opts.lockfileDir, lockfileOpts));
        }
    }
    else {
        if (await lockfile_file_1.existsWantedLockfile(opts.lockfileDir)) {
            logger_1.default.warn({
                message: `A ${constants_1.WANTED_LOCKFILE} file exists. The current configuration prohibits to read or write a lockfile`,
                prefix: opts.lockfileDir,
            });
        }
        fileReads.push(Promise.resolve(undefined));
    }
    fileReads.push(lockfile_file_1.readCurrentLockfile(opts.virtualStoreDir, lockfileOpts));
    // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
    const files = await Promise.all(fileReads);
    const sopts = { lockfileVersion: constants_1.LOCKFILE_VERSION };
    const importerIds = opts.projects.map((importer) => importer.id);
    const currentLockfile = (_a = files[1]) !== null && _a !== void 0 ? _a : lockfile_file_1.createLockfileObject(importerIds, sopts);
    for (const importerId of importerIds) {
        if (!currentLockfile.importers[importerId]) {
            currentLockfile.importers[importerId] = {
                specifiers: {},
            };
        }
    }
    const wantedLockfile = (_c = (_b = files[0]) !== null && _b !== void 0 ? _b : (currentLockfile && R.clone(currentLockfile))) !== null && _c !== void 0 ? _c : lockfile_file_1.createLockfileObject(importerIds, sopts);
    for (const importerId of importerIds) {
        if (!wantedLockfile.importers[importerId]) {
            wantedLockfile.importers[importerId] = {
                specifiers: {},
            };
        }
    }
    return {
        currentLockfile,
        currentLockfileIsUpToDate: R.equals(currentLockfile, wantedLockfile),
        existsCurrentLockfile: !!files[1],
        existsWantedLockfile: !!files[0],
        wantedLockfile,
        lockfileHadConflicts,
    };
}
exports.default = default_1;
//# sourceMappingURL=readLockfiles.js.map