"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PnpmOptionsConfiguration = void 0;
const node_core_library_1 = require("@rushstack/node-core-library");
const heft_config_file_1 = require("@rushstack/heft-config-file");
const terminal_1 = require("@rushstack/terminal");
const BasePackageManagerOptionsConfiguration_1 = require("../base/BasePackageManagerOptionsConfiguration");
const EnvironmentConfiguration_1 = require("../../api/EnvironmentConfiguration");
const pnpm_config_schema_json_1 = __importDefault(require("../../schemas/pnpm-config.schema.json"));
/**
 * Options that are only used when the PNPM package manager is selected.
 * Use this class to load "common/config/rush/pnpm-config.json" file,
 * or, load json from "pnpmOptions" field in "rush.json" for legacy support.
 *
 * @remarks
 * It is valid to define these options in rush.json even if the PNPM package manager
 * is not being used.
 *
 * @public
 */
class PnpmOptionsConfiguration extends BasePackageManagerOptionsConfiguration_1.PackageManagerOptionsConfigurationBase {
    /**
     * (GENERATED BY RUSH-PNPM PATCH-COMMIT) When modifying this property, make sure you know what you are doing.
     *
     * The `globalPatchedDependencies` is added/updated automatically when you run pnpm patch-commit
     * command. It is a dictionary where the key should be the package name and exact version. The value
     * should be a relative path to a patch file.
     *
     * PNPM documentation: https://pnpm.io/package_json#pnpmpatcheddependencies
     */
    get globalPatchedDependencies() {
        return this._globalPatchedDependencies;
    }
    constructor(json, commonTempFolder, jsonFilename) {
        super(json);
        this._json = json;
        this.jsonFilename = jsonFilename;
        this.pnpmStore = json.pnpmStore || 'local';
        if (EnvironmentConfiguration_1.EnvironmentConfiguration.pnpmStorePathOverride) {
            this.pnpmStorePath = EnvironmentConfiguration_1.EnvironmentConfiguration.pnpmStorePathOverride;
        }
        else if (this.pnpmStore === 'global') {
            this.pnpmStorePath = '';
        }
        else {
            this.pnpmStorePath = `${commonTempFolder}/pnpm-store`;
        }
        this.strictPeerDependencies = !!json.strictPeerDependencies;
        this.preventManualShrinkwrapChanges = !!json.preventManualShrinkwrapChanges;
        this.useWorkspaces = !!json.useWorkspaces;
        this.globalOverrides = json.globalOverrides;
        this.globalPeerDependencyRules = json.globalPeerDependencyRules;
        this.globalPackageExtensions = json.globalPackageExtensions;
        this.globalNeverBuiltDependencies = json.globalNeverBuiltDependencies;
        this.globalOnlyBuiltDependencies = json.globalOnlyBuiltDependencies;
        this.globalIgnoredOptionalDependencies = json.globalIgnoredOptionalDependencies;
        this.globalAllowedDeprecatedVersions = json.globalAllowedDeprecatedVersions;
        this.unsupportedPackageJsonSettings = json.unsupportedPackageJsonSettings;
        this._globalPatchedDependencies = json.globalPatchedDependencies;
        this.resolutionMode = json.resolutionMode;
        this.autoInstallPeers = json.autoInstallPeers;
        this.minimumReleaseAge = json.minimumReleaseAge;
        this.minimumReleaseAgeExclude = json.minimumReleaseAgeExclude;
        this.alwaysInjectDependenciesFromOtherSubspaces = json.alwaysInjectDependenciesFromOtherSubspaces;
        this.alwaysFullInstall = json.alwaysFullInstall;
        this.pnpmLockfilePolicies = json.pnpmLockfilePolicies;
        this.globalCatalogs = json.globalCatalogs;
    }
    /** @internal */
    static loadFromJsonFileOrThrow(jsonFilePath, commonTempFolder) {
        // TODO: plumb through the terminal
        const terminal = new terminal_1.Terminal(new terminal_1.ConsoleTerminalProvider());
        const pnpmOptionsConfigFile = new heft_config_file_1.NonProjectConfigurationFile({
            jsonSchemaObject: pnpm_config_schema_json_1.default
        });
        const pnpmConfigJson = pnpmOptionsConfigFile.loadConfigurationFile(terminal, jsonFilePath);
        pnpmConfigJson.$schema = pnpmOptionsConfigFile.getSchemaPropertyOriginalValue(pnpmConfigJson);
        return new PnpmOptionsConfiguration(pnpmConfigJson || {}, commonTempFolder, jsonFilePath);
    }
    /** @internal */
    static loadFromJsonObject(json, commonTempFolder) {
        return new PnpmOptionsConfiguration(json, commonTempFolder);
    }
    /**
     * Updates patchedDependencies field of the PNPM options in the common/config/rush/pnpm-config.json file.
     */
    updateGlobalPatchedDependencies(patchedDependencies) {
        this._globalPatchedDependencies = patchedDependencies;
        this._json.globalPatchedDependencies = patchedDependencies;
        if (this.jsonFilename) {
            node_core_library_1.JsonFile.save(this._json, this.jsonFilename, { updateExistingFile: true });
        }
    }
    /**
     * Updates globalOnlyBuiltDependencies field of the PNPM options in the common/config/rush/pnpm-config.json file.
     */
    updateGlobalOnlyBuiltDependencies(onlyBuiltDependencies) {
        this._json.globalOnlyBuiltDependencies = onlyBuiltDependencies;
        if (this.jsonFilename) {
            node_core_library_1.JsonFile.save(this._json, this.jsonFilename, { updateExistingFile: true });
        }
    }
}
exports.PnpmOptionsConfiguration = PnpmOptionsConfiguration;
//# sourceMappingURL=PnpmOptionsConfiguration.js.map