"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveRegistryUrl = exports.loadConfigFromYarnrcYml = void 0;
const js_yaml_1 = require("js-yaml");
const zod_1 = require("zod");
const logger_1 = require("../../../../logger");
const YarnrcYmlSchema = zod_1.z.object({
    npmRegistryServer: zod_1.z.string().optional(),
    npmScopes: zod_1.z
        .record(zod_1.z.object({
        npmRegistryServer: zod_1.z.string().optional(),
    }))
        .optional(),
});
function loadConfigFromYarnrcYml(yarnrcYml) {
    try {
        const obj = (0, js_yaml_1.load)(yarnrcYml, {
            json: true,
        });
        if (!obj) {
            // emtpy yaml file
            return null;
        }
        return YarnrcYmlSchema.parse(obj);
    }
    catch (err) {
        logger_1.logger.warn({ yarnrcYml, err }, `Failed to load yarnrc file`);
        return null;
    }
}
exports.loadConfigFromYarnrcYml = loadConfigFromYarnrcYml;
function resolveRegistryUrl(packageName, yarnConfig) {
    if (yarnConfig.npmScopes) {
        for (const scope in yarnConfig.npmScopes) {
            if (packageName.startsWith(`@${scope}/`)) {
                return yarnConfig.npmScopes[scope].npmRegistryServer ?? null;
            }
        }
    }
    if (yarnConfig.npmRegistryServer) {
        return yarnConfig.npmRegistryServer;
    }
    return null;
}
exports.resolveRegistryUrl = resolveRegistryUrl;
//# sourceMappingURL=yarnrc.js.map