"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getExcludeTasksFromTaskGraph = getExcludeTasksFromTaskGraph;
exports.getAllDependsOnFromTaskGraph = getAllDependsOnFromTaskGraph;
function getGradleTaskName(target, nodes) {
    return nodes[target.project]?.data?.targets?.[target.target]?.options
        ?.taskName;
}
function getExcludeTasksFromTaskGraph(taskIdsToExcludeDepsOf, runningTaskIds, taskGraph, nodes, includeDependsOnTasks = new Set()) {
    const excludes = new Set();
    const transitiveDepIds = getAllDependsOnFromTaskGraph(taskIdsToExcludeDepsOf, taskGraph);
    for (const depTaskId of transitiveDepIds) {
        if (runningTaskIds.has(depTaskId)) {
            continue;
        }
        const task = taskGraph.tasks[depTaskId];
        if (!task) {
            continue;
        }
        const gradleTaskName = getGradleTaskName(task.target, nodes);
        if (gradleTaskName && !includeDependsOnTasks.has(gradleTaskName)) {
            excludes.add(gradleTaskName);
        }
    }
    return excludes;
}
function getAllDependsOnFromTaskGraph(startTaskIds, taskGraph) {
    const result = new Set();
    const seen = new Set();
    const stack = [];
    const edges = (id) => [
        ...(taskGraph.dependencies[id] ?? []),
        ...(taskGraph.continuousDependencies?.[id] ?? []),
    ];
    for (const id of startTaskIds) {
        seen.add(id);
        for (const dep of edges(id)) {
            stack.push(dep);
        }
    }
    while (stack.length > 0) {
        const current = stack.pop();
        if (seen.has(current)) {
            continue;
        }
        seen.add(current);
        result.add(current);
        for (const dep of edges(current)) {
            if (!seen.has(dep)) {
                stack.push(dep);
            }
        }
    }
    return result;
}
