"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPackageManagerVersion = exports.loadPackageJson = exports.lazyLoadPackageJson = void 0;
const tslib_1 = require("tslib");
const upath_1 = tslib_1.__importDefault(require("upath"));
const fs_1 = require("../../../../util/fs");
const lazy_1 = require("../../../../util/lazy");
const schema_1 = require("../schema");
function lazyLoadPackageJson(lockFileDir) {
    return new lazy_1.Lazy(() => loadPackageJson(lockFileDir));
}
exports.lazyLoadPackageJson = lazyLoadPackageJson;
async function loadPackageJson(lockFileDir) {
    const json = await (0, fs_1.readLocalFile)(upath_1.default.join(lockFileDir, 'package.json'), 'utf8');
    const res = schema_1.PackageJson.safeParse(json);
    if (res.success) {
        return res.data;
    }
    return {};
}
exports.loadPackageJson = loadPackageJson;
function getPackageManagerVersion(name, pkg) {
    if (pkg.packageManager?.name === name) {
        return pkg.packageManager.version;
    }
    if (pkg.engines?.[name]) {
        return pkg.engines[name];
    }
    return null;
}
exports.getPackageManagerVersion = getPackageManagerVersion;
//# sourceMappingURL=utils.js.map