// engine/ai-cost-estimator/recommender.ts — Model recommendation and markdown formatting.
// Extracted from core.ts to keep file sizes within limits.
/** Selects the recommended model based on spec complexity. */
export function selectRecommendedModel(models, complexity) {
    const preferencesByComplexity = {
        low: [
            'claude-haiku-3-5',
            'gemini-2-5-pro',
            'gpt-4-1',
            'gpt-4o',
            'claude-sonnet-4',
            'claude-opus-4',
        ],
        medium: [
            'claude-sonnet-4',
            'gpt-4o',
            'gpt-4-1',
            'gemini-2-5-pro',
            'claude-haiku-3-5',
            'claude-opus-4',
        ],
        high: [
            'claude-opus-4',
            'claude-sonnet-4',
            'gpt-4o',
            'gpt-4-1',
            'gemini-2-5-pro',
            'claude-haiku-3-5',
        ],
    };
    const preferences = preferencesByComplexity[complexity] ?? preferencesByComplexity.medium ?? [];
    for (const modelId of preferences) {
        const model = models.find((m) => m.id === modelId);
        if (model) {
            return model;
        }
    }
    // Fallback: cheapest model
    /* v8 ignore start */
    const sorted = [...models].sort((a, b) => a.inputPricePerMillion - b.inputPricePerMillion);
    const first = sorted[0];
    if (!first) {
        throw new Error('No models available in pricing config');
    }
    return first;
    /* v8 ignore stop */
}
/** Builds a human-readable recommendation reason. */
export function buildRecommendationReason(model, complexity, savingsVsMostExpensive) {
    const complexityLabel = { low: 'baja', medium: 'media', high: 'alta' }[complexity];
    const baseReason = `Para complejidad ${complexityLabel}, **${model.displayName}** ofrece `;
    if (complexity === 'low') {
        return (baseReason +
            `el mejor precio sin sacrificar calidad. ` +
            `Ahorro estimado vs modelo más caro: $${savingsVsMostExpensive.toFixed(4)} USD.`);
    }
    if (complexity === 'medium') {
        return (baseReason +
            `el mejor balance calidad/precio. ` +
            `Ahorro estimado vs modelo más caro: $${savingsVsMostExpensive.toFixed(4)} USD.`);
    }
    return (baseReason +
        `máxima calidad para specs complejas. ` +
        `El costo adicional se justifica por la reducción de iteraciones.`);
}
/** Formats the cost estimates as a markdown table. */
export function formatMarkdownTable(estimates, recommendedModelId, recommendationReason, staleWarning) {
    const header = '| Modelo | Tokens Input | Tokens Output | Costo Estimado | Rango (±) | Agent Turns |\n' +
        '|--------|-------------|---------------|----------------|-----------|-------------|';
    const rows = estimates.map((est) => {
        const marker = est.modelId === recommendedModelId ? ' ⭐' : '';
        const cost = `$${est.estimatedCostUsd.toFixed(4)}`;
        const range = `±${est.confidenceRangePct}%`;
        return (`| ${est.displayName}${marker} | ${est.inputTokens.toLocaleString()} | ` +
            `${est.outputTokens.toLocaleString()} | ${cost} | ${range} | ${est.agentTurns} |`);
    });
    const recommended = estimates.find((e) => e.modelId === recommendedModelId);
    const recommendedName = recommended?.displayName ?? recommendedModelId;
    const lines = [
        '## Estimación de Costo por Modelo IA',
        '',
        header,
        ...rows,
        '',
        `## Recomendación: ${recommendedName}`,
        '',
        recommendationReason,
    ];
    if (staleWarning) {
        lines.push('', `> **Aviso**: ${staleWarning}`);
    }
    return lines.join('\n');
}
//# sourceMappingURL=recommender.js.map