"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getExcludeTasks = getExcludeTasks;
exports.getGradleTaskNameWithNxTaskId = getGradleTaskNameWithNxTaskId;
exports.getAllDependsOn = getAllDependsOn;
/**
 * Returns Gradle CLI arguments to exclude dependent tasks
 * that are not part of the current execution set.
 *
 * For example, if a project defines `dependsOn: ['lint']` for the `test` target,
 * and only `test` is running, this will return: ['lint']
 *
 * @param taskIds - Set of Nx task IDs to process
 * @param nodes - Project graph nodes
 * @param runningTaskIds - Set of task IDs that are currently running (won't be excluded)
 * @param includeDependsOnTasks - Set of Gradle task names that should be included (not excluded)
 *   (typically provider-based dependencies that Gradle must resolve)
 */
function getExcludeTasks(taskIds, nodes, runningTaskIds = new Set(), includeDependsOnTasks = new Set()) {
    const excludes = new Set();
    for (const taskId of taskIds) {
        const [project, target] = taskId.split(':');
        const taskDeps = nodes[project]?.data?.targets?.[target]?.dependsOn ?? [];
        for (const dep of taskDeps) {
            const taskId = typeof dep === 'string' ? dep : dep?.target;
            if (taskId && !runningTaskIds.has(taskId)) {
                const gradleTaskName = getGradleTaskNameWithNxTaskId(taskId, nodes);
                if (gradleTaskName && !includeDependsOnTasks.has(gradleTaskName)) {
                    excludes.add(gradleTaskName);
                }
            }
        }
    }
    return excludes;
}
function getGradleTaskNameWithNxTaskId(nxTaskId, nodes) {
    const [projectName, targetName] = nxTaskId.split(':');
    const gradleTaskName = nodes[projectName]?.data?.targets?.[targetName]?.options?.taskName;
    return gradleTaskName;
}
function getAllDependsOn(nodes, projectName, targetName) {
    const allDependsOn = new Set();
    const stack = [`${projectName}:${targetName}`];
    while (stack.length > 0) {
        const currentTaskId = stack.pop();
        if (currentTaskId && !allDependsOn.has(currentTaskId)) {
            allDependsOn.add(currentTaskId);
            const [currentProjectName, currentTargetName] = currentTaskId.split(':');
            const directDependencies = nodes[currentProjectName]?.data?.targets?.[currentTargetName]
                ?.dependsOn ?? [];
            for (const dep of directDependencies) {
                const depTaskId = typeof dep === 'string' ? dep : dep?.target;
                if (depTaskId && !allDependsOn.has(depTaskId)) {
                    stack.push(depTaskId);
                }
            }
        }
    }
    allDependsOn.delete(`${projectName}:${targetName}`); // Exclude the starting task itself
    return allDependsOn;
}
