"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
Object.defineProperty(exports, "__esModule", { value: true });
exports.WeightedOperationPlugin = void 0;
const node_core_library_1 = require("@rushstack/node-core-library");
const PLUGIN_NAME = 'WeightedOperationPlugin';
/**
 * Add weights to operations based on the operation settings in rush-project.json.
 *
 * This also sets the weight of no-op operations to 0.
 */
class WeightedOperationPlugin {
    apply(hooks) {
        hooks.beforeExecuteOperations.tap(PLUGIN_NAME, weightOperations);
    }
}
exports.WeightedOperationPlugin = WeightedOperationPlugin;
function weightOperations(operations, context) {
    var _a;
    const { projectConfigurations } = context;
    for (const [operation, record] of operations) {
        const { runner } = record;
        const { associatedProject: project, associatedPhase: phase } = operation;
        if (runner.isNoOp) {
            operation.weight = 0;
        }
        else {
            const projectConfiguration = projectConfigurations.get(project);
            const operationSettings = (_a = operation.settings) !== null && _a !== void 0 ? _a : projectConfiguration === null || projectConfiguration === void 0 ? void 0 : projectConfiguration.operationSettingsByOperationName.get(phase.name);
            if (operationSettings === null || operationSettings === void 0 ? void 0 : operationSettings.weight) {
                operation.weight = operationSettings.weight;
            }
        }
        node_core_library_1.Async.validateWeightedIterable(operation);
    }
    return operations;
}
//# sourceMappingURL=WeightedOperationPlugin.js.map