// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { JsonFile } from '@rushstack/node-core-library';
import { NonProjectConfigurationFile } from '@rushstack/heft-config-file';
import { ConsoleTerminalProvider, Terminal } from '@rushstack/terminal';
import { PackageManagerOptionsConfigurationBase } from '../base/BasePackageManagerOptionsConfiguration';
import { EnvironmentConfiguration } from '../../api/EnvironmentConfiguration';
import schemaJson from '../../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
 */
export class PnpmOptionsConfiguration extends 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.pnpmStorePathOverride) {
            this.pnpmStorePath = 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(new ConsoleTerminalProvider());
        const pnpmOptionsConfigFile = new NonProjectConfigurationFile({
            jsonSchemaObject: schemaJson
        });
        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) {
            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) {
            JsonFile.save(this._json, this.jsonFilename, { updateExistingFile: true });
        }
    }
}
//# sourceMappingURL=PnpmOptionsConfiguration.js.map