const BASE_SCORE = 50;
const HISTORICAL_ADJUSTMENT = 20;
function buildPositiveFactors(ctx) {
    const factors = [];
    if (ctx.scope !== 'architectural') {
        factors.push({
            name: 'Well-defined scope',
            impact: 'positive',
            weight: 0.2,
            description: `Scope "${ctx.scope}" is clearly bounded`,
        });
    }
    if (ctx.framework !== null) {
        factors.push({
            name: 'Known framework',
            impact: 'positive',
            weight: 0.15,
            description: `Framework "${ctx.framework}" is established and well-understood`,
        });
    }
    if (ctx.dependencyCount < 3) {
        factors.push({
            name: 'Low dependency count',
            impact: 'positive',
            weight: 0.1,
            description: `Only ${ctx.dependencyCount} dependencies reduces integration risk`,
        });
    }
    if (ctx.difficulty <= 2) {
        factors.push({
            name: 'Low difficulty',
            impact: 'positive',
            weight: 0.15,
            description: `Difficulty level ${ctx.difficulty}/5 is well within team capability`,
        });
    }
    return factors;
}
function buildNegativeFactors(ctx) {
    const factors = [];
    if (ctx.framework === null) {
        factors.push({
            name: 'Unknown framework',
            impact: 'negative',
            weight: 0.2,
            description: 'No framework specified increases estimation uncertainty',
        });
    }
    if (ctx.dependencyCount > 5) {
        factors.push({
            name: 'Many dependencies',
            impact: 'negative',
            weight: 0.15,
            description: `${ctx.dependencyCount} dependencies increase coordination risk`,
        });
    }
    if (ctx.scope === 'cross-module' || ctx.scope === 'architectural') {
        factors.push({
            name: 'Broad scope',
            impact: 'negative',
            weight: 0.2,
            description: `Scope "${ctx.scope}" spans multiple areas making estimation harder`,
        });
    }
    if (ctx.risk === 'high' || ctx.risk === 'critical') {
        factors.push({
            name: 'High risk level',
            impact: 'negative',
            weight: 0.15,
            description: `Risk level "${ctx.risk}" signals higher uncertainty`,
        });
    }
    return factors;
}
function computeScore(positiveFactors, negativeFactors, historicalAccuracy) {
    let score = BASE_SCORE;
    for (const f of positiveFactors) {
        score += f.weight * 100;
    }
    for (const f of negativeFactors) {
        score -= f.weight * 100;
    }
    if (historicalAccuracy !== undefined) {
        const adjustment = (historicalAccuracy - 0.5) * 2 * HISTORICAL_ADJUSTMENT;
        score += adjustment;
    }
    return Math.min(Math.max(score, 0), 100);
}
function scoreToLevel(score) {
    if (score < 35) {
        return 'low';
    }
    if (score <= 65) {
        return 'medium';
    }
    return 'high';
}
/**
 * Calculate the estimation confidence for a spec.
 * @param historicalAccuracy - optional 0-1 ratio from past estimates (1 = perfect accuracy)
 */
export function calculateConfidence(ctx, historicalAccuracy) {
    const positiveFactors = buildPositiveFactors(ctx);
    const negativeFactors = buildNegativeFactors(ctx);
    const allFactors = [...positiveFactors, ...negativeFactors];
    const score = computeScore(positiveFactors, negativeFactors, historicalAccuracy);
    return {
        level: scoreToLevel(score),
        score,
        factors: allFactors,
    };
}
//# sourceMappingURL=confidence-scorer.js.map