/**
 * CheckAndUnblockFeaturesUseCase
 *
 * Evaluates whether blocked direct children of a parent feature are now
 * eligible to start, and if so transitions them to Started and spawns their
 * agents.
 *
 * Business Rules:
 * - Only direct children of parentFeatureId are evaluated (no recursive traversal).
 *   Grandchildren stay Blocked until their own direct parent progresses.
 * - Gate: parent lifecycle must be in POST_IMPLEMENTATION (Implementation, Review, Maintain).
 * - Idempotent: already-Started children are not touched; calling execute() twice is safe.
 * - spawn() is skipped for children missing agentRunId or specPath (defensive guard).
 *
 * Called from: UpdateFeatureLifecycleUseCase after every lifecycle transition.
 */
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';
import { SdlcLifecycle } from '../../../domain/generated/output.js';
import { POST_IMPLEMENTATION } from '../../../domain/lifecycle-gates.js';
let CheckAndUnblockFeaturesUseCase = class CheckAndUnblockFeaturesUseCase {
    featureRepo;
    agentProcess;
    constructor(featureRepo, agentProcess) {
        this.featureRepo = featureRepo;
        this.agentProcess = agentProcess;
    }
    /**
     * Check and unblock direct children of the given parent feature.
     *
     * @param parentFeatureId - ID of the feature whose children should be evaluated.
     */
    async execute(parentFeatureId) {
        // Load parent and verify gate
        const parent = await this.featureRepo.findById(parentFeatureId);
        if (!parent || !POST_IMPLEMENTATION.has(parent.lifecycle)) {
            return;
        }
        // Load direct children
        const children = await this.featureRepo.findByParentId(parentFeatureId);
        // Unblock each blocked child
        for (const child of children) {
            if (child.lifecycle !== SdlcLifecycle.Blocked) {
                continue;
            }
            // Transition to Started
            child.lifecycle = SdlcLifecycle.Started;
            child.updatedAt = new Date();
            await this.featureRepo.update(child);
            // Spawn agent using fields set at feature creation time
            if (child.agentRunId && child.specPath) {
                this.agentProcess.spawn(child.id, child.agentRunId, child.repositoryPath, child.specPath, child.worktreePath, {
                    approvalGates: child.approvalGates,
                    push: child.push,
                    openPr: child.openPr,
                    forkAndPr: child.forkAndPr,
                    commitSpecs: child.commitSpecs,
                    ciWatchEnabled: child.ciWatchEnabled,
                    enableEvidence: child.enableEvidence,
                    commitEvidence: child.commitEvidence,
                    ...(child.fast ? { fast: true } : {}),
                });
            }
        }
    }
};
CheckAndUnblockFeaturesUseCase = __decorate([
    injectable(),
    __param(0, inject('IFeatureRepository')),
    __param(1, inject('IFeatureAgentProcessService')),
    __metadata("design:paramtypes", [Object, Object])
], CheckAndUnblockFeaturesUseCase);
export { CheckAndUnblockFeaturesUseCase };
