"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.write = exports.read = void 0;
const read_yaml_file_1 = __importDefault(require("read-yaml-file"));
const path = require("path");
const isWindows = require("is-windows");
const writeYamlFile = require("write-yaml-file");
// The dot prefix is needed because otherwise `npm shrinkwrap`
// thinks that it is an extraneous package.
const MODULES_FILENAME = '.modules.yaml';
async function read(modulesDir) {
    const modulesYamlPath = path.join(modulesDir, MODULES_FILENAME);
    let modules;
    try {
        modules = await read_yaml_file_1.default(modulesYamlPath);
    }
    catch (err) {
        if (err.code !== 'ENOENT') {
            throw err;
        }
        return null;
    }
    if (!modules.virtualStoreDir) {
        modules.virtualStoreDir = path.join(modulesDir, '.pnpm');
    }
    else if (!path.isAbsolute(modules.virtualStoreDir)) {
        modules.virtualStoreDir = path.join(modulesDir, modules.virtualStoreDir);
    }
    switch (modules.shamefullyHoist) {
        case true:
            if (!modules.publicHoistPattern) {
                modules.publicHoistPattern = ['*'];
            }
            if (modules.hoistedAliases && !modules.hoistedDependencies) {
                modules.hoistedDependencies = {};
                for (const depPath of Object.keys(modules.hoistedAliases)) {
                    modules.hoistedDependencies[depPath] = {};
                    for (const alias of modules.hoistedAliases[depPath]) {
                        modules.hoistedDependencies[depPath][alias] = 'public';
                    }
                }
            }
            break;
        case false:
            if (!modules.publicHoistPattern) {
                modules.publicHoistPattern = [];
            }
            if (modules.hoistedAliases && !modules.hoistedDependencies) {
                modules.hoistedDependencies = {};
                for (const depPath of Object.keys(modules.hoistedAliases)) {
                    modules.hoistedDependencies[depPath] = {};
                    for (const alias of modules.hoistedAliases[depPath]) {
                        modules.hoistedDependencies[depPath][alias] = 'private';
                    }
                }
            }
            break;
    }
    return modules;
}
exports.read = read;
const YAML_OPTS = {
    noCompatMode: true,
    noRefs: true,
    sortKeys: true,
};
function write(modulesDir, modules) {
    const modulesYamlPath = path.join(modulesDir, MODULES_FILENAME);
    const saveModules = { ...modules };
    if (saveModules.skipped)
        saveModules.skipped.sort();
    if (!saveModules.hoistPattern) {
        // Because the YAML writer fails on undefined fields
        delete saveModules.hoistPattern;
    }
    if (!saveModules.publicHoistPattern) {
        delete saveModules.publicHoistPattern;
    }
    if (!saveModules.hoistedAliases || !saveModules.hoistPattern && !saveModules.publicHoistPattern) {
        delete saveModules.hoistedAliases;
    }
    // We should store the absolute virtual store directory path on Windows
    // because junctions are used on Windows. Junctions will break even if
    // the relative path to the virtual store remains the same after moving
    // a project.
    if (!isWindows()) {
        saveModules.virtualStoreDir = path.relative(modulesDir, saveModules.virtualStoreDir);
    }
    return writeYamlFile(modulesYamlPath, saveModules, YAML_OPTS);
}
exports.write = write;
//# sourceMappingURL=index.js.map