"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 installChecks = require("pnpm-install-checks");
const loggers_1 = require("../loggers");
const nodeIdUtils_1 = require("../nodeIdUtils");
function getIsInstallable(pkgId, pkg, options) {
    return __awaiter(this, void 0, void 0, function* () {
        const warn = (yield installChecks.checkPlatform(pkg)) || (yield installChecks.checkEngine(pkg, {
            nodeVersion: options.nodeVersion,
            pnpmVersion: options.pnpmVersion,
        }));
        if (!warn)
            return true;
        loggers_1.installCheckLogger.warn(warn);
        if (options.optional) {
            const friendlyPath = nodeIdToFriendlyPath(options.nodeId, options.pkgByPkgId);
            logger_1.default.warn({
                message: `${friendlyPath ? `${friendlyPath}: ` : ''}Skipping failed optional dependency ${pkg.name}@${pkg.version}`,
                warn,
            });
            return false;
        }
        if (options.engineStrict)
            throw warn;
        return true;
    });
}
exports.default = getIsInstallable;
function nodeIdToFriendlyPath(nodeId, pkgByPkgId) {
    const pkgIds = nodeIdUtils_1.splitNodeId(nodeId).slice(2, -2);
    return pkgIds
        .map((pkgId) => pkgByPkgId[pkgId].name)
        .join(' > ');
}
//# sourceMappingURL=getIsInstallable.js.map