"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.distributeWorkerSlots = distributeWorkerSlots;
function distributeWorkerSlots(input) {
    const launchableIssueCount = Math.min(input.issueCount, input.maxIssues, input.maxWorkers);
    if (launchableIssueCount <= 0) {
        return [];
    }
    if (input.mode === "greedy") {
        return distributeGreedyWorkerSlots({
            issueCount: launchableIssueCount,
            maxWorkers: input.maxWorkers,
            issueDemands: input.issueDemands ?? [],
        });
    }
    const baseSlots = Math.max(1, Math.floor(input.maxWorkers / launchableIssueCount));
    let remainingExtraSlots = Math.max(0, input.maxWorkers - baseSlots * launchableIssueCount);
    return Array.from({ length: launchableIssueCount }, () => {
        const extraSlot = remainingExtraSlots > 0 ? 1 : 0;
        remainingExtraSlots -= extraSlot;
        return baseSlots + extraSlot;
    });
}
function distributeGreedyWorkerSlots(input) {
    let remainingWorkers = input.maxWorkers;
    const slotCounts = [];
    for (let index = 0; index < input.issueCount; index += 1) {
        if (remainingWorkers <= 0) {
            break;
        }
        const issueDemand = normalizeIssueDemand(input.issueDemands[index]);
        const assignedSlots = Math.min(issueDemand, remainingWorkers);
        slotCounts.push(assignedSlots);
        remainingWorkers -= assignedSlots;
    }
    return slotCounts;
}
function normalizeIssueDemand(demand) {
    if (demand === undefined || !Number.isFinite(demand)) {
        return 1;
    }
    return Math.max(1, Math.floor(demand));
}
