"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseParallelism = parseParallelism;
const os = __importStar(require("node:os"));
const executionUtilities_1 = require("../../utilities/executionUtilities");
/**
 * Parses a command line specification for desired parallelism.
 * Factored out to enable unit tests
 */
function parseParallelism(rawParallelism, numberOfCores) {
    var _a, _b;
    if (numberOfCores === void 0) { numberOfCores = (_b = (_a = os.availableParallelism) === null || _a === void 0 ? void 0 : _a.call(os)) !== null && _b !== void 0 ? _b : os.cpus().length; }
    if (rawParallelism) {
        if (rawParallelism === 'max') {
            return numberOfCores;
        }
        else {
            const parallelismAsNumber = Number(rawParallelism);
            if (typeof rawParallelism === 'string' && rawParallelism.trim().endsWith('%')) {
                const parsedPercentage = Number(rawParallelism.trim().replace(/\%$/, ''));
                if (parsedPercentage <= 0 || parsedPercentage > 100) {
                    throw new Error(`Invalid percentage value of '${rawParallelism}', value cannot be less than '0%' or more than '100%'`);
                }
                const workers = Math.floor((parsedPercentage / 100) * numberOfCores);
                return Math.max(workers, 1);
            }
            else if (!isNaN(parallelismAsNumber)) {
                return Math.max(parallelismAsNumber, 1);
            }
            else {
                throw new Error(`Invalid parallelism value of '${rawParallelism}', expected a number, a percentage, or 'max'`);
            }
        }
    }
    else {
        // If an explicit parallelism number wasn't provided, then choose a sensible
        // default.
        if (executionUtilities_1.IS_WINDOWS) {
            // On desktop Windows, some people have complained that their system becomes
            // sluggish if Rush is using all the CPU cores.  Leave one thread for
            // other operations. For CI environments, you can use the "max" argument to use all available cores.
            return Math.max(numberOfCores - 1, 1);
        }
        else {
            // Unix-like operating systems have more balanced scheduling, so default
            // to the number of CPU cores
            return numberOfCores;
        }
    }
}
//# sourceMappingURL=ParseParallelism.js.map