function buildRiskTemplates(ctx) {
    const templates = [];
    if (ctx.difficulty >= 4) {
        templates.push({
            description: 'High complexity may lead to estimation overrun',
            category: 'technical',
            probability: 40,
            impactDays: 3,
            mitigationPlan: 'Break down into smaller tasks and reassess estimates',
        });
    }
    if (ctx.scope === 'cross-module' || ctx.scope === 'architectural') {
        templates.push({
            description: 'Cross-module changes risk breaking existing integrations',
            category: 'integration',
            probability: 35,
            impactDays: 2,
            mitigationPlan: 'Add integration tests covering all affected modules before merging',
        });
    }
    if (ctx.dependencyCount > 3) {
        templates.push({
            description: 'Multiple dependencies increase coordination overhead',
            category: 'dependency',
            probability: 30,
            impactDays: 2,
            mitigationPlan: 'Establish dependency contracts early and monitor for version conflicts',
        });
    }
    if (ctx.risk === 'high' || ctx.risk === 'critical') {
        templates.push({
            description: 'Spec flagged as high-risk requires extra review',
            category: 'organizational',
            probability: 50,
            impactDays: 3,
            mitigationPlan: 'Schedule mandatory peer review and add approval gates before deployment',
        });
    }
    if (ctx.tags.includes('security')) {
        templates.push({
            description: 'Security-sensitive changes require audit',
            category: 'security',
            probability: 25,
            impactDays: 4,
            mitigationPlan: 'Engage security team for review and run SAST/DAST tools',
        });
    }
    if (ctx.fileCount > 10) {
        templates.push({
            description: 'Large number of files increases merge conflict risk',
            category: 'technical',
            probability: 30,
            impactDays: 1,
            mitigationPlan: 'Use feature flags and incremental PRs to reduce merge surface',
        });
    }
    return templates;
}
function buildRisks(ctx, templates) {
    return templates.map((t, index) => ({
        id: `R${index + 1}`,
        description: t.description,
        category: t.category,
        probability: t.probability,
        impactUsd: 0,
        impactDays: t.impactDays,
        owner: '',
        status: 'identified',
        mitigationPlan: t.mitigationPlan,
        specId: ctx.specId,
    }));
}
function computeAggregateScore(risks, totalBudgetDays) {
    const rawScore = risks.reduce((acc, r) => acc + (r.probability / 100) * r.impactDays, 0);
    const denominator = Math.max(totalBudgetDays, 1);
    const score = (rawScore / denominator) * 100;
    return Math.min(score, 100);
}
/**
 * Generate an initial risk register from spec metadata.
 */
export function generateRiskRegister(ctx, totalBudget) {
    const budgetDays = totalBudget ?? 30;
    const templates = buildRiskTemplates(ctx);
    const risks = buildRisks(ctx, templates);
    const aggregateRiskScore = computeAggregateScore(risks, budgetDays);
    return {
        specId: ctx.specId,
        risks,
        aggregateRiskScore,
        generatedAt: new Date().toISOString(),
    };
}
//# sourceMappingURL=risk-generator.js.map