/**
 * List SDLC Board Use Case
 *
 * Builds a ready-to-render board data structure:
 *   - Epics = Features (non-deleted)
 *   - Each epic contains tasks sorted by sortOrder ASC
 *   - Each task node contains its sub-tasks sorted by sortOrder ASC
 *
 * Avoids N+1 by fetching all active tasks in one call, then fetching
 * sub-tasks per feature (one call per feature with tasks, or all features
 * with a denormalized featureId lookup).
 */
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
    return function (target, key) { decorator(target, key, paramIndex); }
};
import { injectable, inject } from 'tsyringe';
let ListSdlcBoardUseCase = class ListSdlcBoardUseCase {
    featureRepo;
    taskRepo;
    subTaskRepo;
    constructor(featureRepo, taskRepo, subTaskRepo) {
        this.featureRepo = featureRepo;
        this.taskRepo = taskRepo;
        this.subTaskRepo = subTaskRepo;
    }
    async execute() {
        // Fetch all non-deleted features as epics
        const features = await this.featureRepo.list({ includeDeleted: false });
        if (features.length === 0) {
            return { epics: [] };
        }
        // Fetch all active tasks in a single query (avoids N+1)
        const allTasks = await this.taskRepo.listAllActive();
        // Group tasks by featureId
        const tasksByFeature = new Map();
        for (const task of allTasks) {
            const existing = tasksByFeature.get(task.featureId);
            if (existing) {
                existing.push(task);
            }
            else {
                tasksByFeature.set(task.featureId, [task]);
            }
        }
        // Build epics in parallel: fetch sub-tasks per feature
        const epics = await Promise.all(features.map(async (feature) => {
            const featureTasks = (tasksByFeature.get(feature.id) ?? [])
                .slice()
                .sort((a, b) => a.sortOrder - b.sortOrder);
            if (featureTasks.length === 0) {
                return { feature, tasks: [] };
            }
            // Fetch sub-tasks for this feature in one call (denormalized featureId)
            const subTasks = await this.subTaskRepo.listByFeature(feature.id);
            // Group sub-tasks by taskId
            const subTasksByTask = new Map();
            for (const sub of subTasks) {
                const existing = subTasksByTask.get(sub.taskId);
                if (existing) {
                    existing.push(sub);
                }
                else {
                    subTasksByTask.set(sub.taskId, [sub]);
                }
            }
            const taskNodes = featureTasks.map((task) => {
                const nested = (subTasksByTask.get(task.id) ?? [])
                    .slice()
                    .sort((a, b) => a.sortOrder - b.sortOrder);
                return { task, subTasks: nested };
            });
            return { feature, tasks: taskNodes };
        }));
        return { epics };
    }
};
ListSdlcBoardUseCase = __decorate([
    injectable(),
    __param(0, inject('IFeatureRepository')),
    __param(1, inject('ISdlcTaskRepository')),
    __param(2, inject('ISdlcSubTaskRepository')),
    __metadata("design:paramtypes", [Object, Object, Object])
], ListSdlcBoardUseCase);
export { ListSdlcBoardUseCase };
