const MIN_HIGH_CONFIDENCE = 50;
const MIN_MEDIUM_CONFIDENCE = 10;
const MIN_LOW_CONFIDENCE = 3;
const TOP_TOOLS_LIMIT = 5;
const MONTHLY_TREND_MONTHS = 6;
function computeAverage(entries) {
    if (entries.length === 0) {
        return 0;
    }
    const sum = entries.reduce((acc, e) => acc + e.totalTokens, 0);
    return sum / entries.length;
}
function computeConfidenceLevel(specGuidedCount, unstructuredCount) {
    const minCount = Math.min(specGuidedCount, unstructuredCount);
    if (minCount === 0) {
        return 'insufficient';
    }
    if (minCount >= MIN_HIGH_CONFIDENCE) {
        return 'high';
    }
    if (minCount >= MIN_MEDIUM_CONFIDENCE) {
        return 'medium';
    }
    if (minCount >= MIN_LOW_CONFIDENCE) {
        return 'low';
    }
    return 'insufficient';
}
function computeSavingsPct(avgSpecGuided, avgUnstructured) {
    if (avgUnstructured === 0) {
        return null;
    }
    return ((avgUnstructured - avgSpecGuided) / avgUnstructured) * 100;
}
function computeTopSavingTools(specGuided, unstructured) {
    const specGuidedByTool = groupByTool(specGuided);
    const unstructuredByTool = groupByTool(unstructured);
    const allTools = new Set([...Object.keys(specGuidedByTool), ...Object.keys(unstructuredByTool)]);
    const results = [];
    for (const tool of allTools) {
        const sg = specGuidedByTool[tool] ?? [];
        const un = unstructuredByTool[tool] ?? [];
        if (sg.length === 0 || un.length === 0) {
            continue;
        }
        const avgSg = computeAverage(sg);
        const avgUn = computeAverage(un);
        const savingsPct = computeSavingsPct(avgSg, avgUn);
        if (savingsPct === null) {
            continue;
        }
        results.push({ tool, savingsPct, specGuided: avgSg, unstructured: avgUn });
    }
    return results.sort((a, b) => b.savingsPct - a.savingsPct).slice(0, TOP_TOOLS_LIMIT);
}
function groupByTool(entries) {
    const groups = {};
    for (const entry of entries) {
        groups[entry.toolName] ??= [];
        const bucket = groups[entry.toolName];
        if (bucket !== undefined) {
            bucket.push(entry);
        }
    }
    return groups;
}
function getMonthKey(timestamp) {
    return timestamp.slice(0, 7); // YYYY-MM
}
function computeMonthlyTrend(specGuided, unstructured) {
    const now = new Date();
    const months = [];
    for (let i = MONTHLY_TREND_MONTHS - 1; i >= 0; i--) {
        const d = new Date(now.getFullYear(), now.getMonth() - i, 1);
        months.push(d.toISOString().slice(0, 7));
    }
    return months.map((month) => {
        const sgForMonth = specGuided.filter((e) => getMonthKey(e.timestamp) === month);
        const unForMonth = unstructured.filter((e) => getMonthKey(e.timestamp) === month);
        if (sgForMonth.length === 0 || unForMonth.length === 0) {
            return { month, savingsPct: null };
        }
        const avgSg = computeAverage(sgForMonth);
        const avgUn = computeAverage(unForMonth);
        return { month, savingsPct: computeSavingsPct(avgSg, avgUn) };
    });
}
export function calculateTokenSavings(entries, period) {
    const specGuided = entries.filter((e) => e.specId !== undefined && e.specId !== '');
    const unstructured = entries.filter((e) => e.specId === undefined || e.specId === '');
    const avgSpecGuided = computeAverage(specGuided);
    const avgUnstructured = computeAverage(unstructured);
    const confidenceLevel = computeConfidenceLevel(specGuided.length, unstructured.length);
    let savingsPct = null;
    if (confidenceLevel !== 'insufficient') {
        savingsPct = computeSavingsPct(avgSpecGuided, avgUnstructured);
    }
    const estimatedSavingsTotal = savingsPct !== null ? (avgUnstructured - avgSpecGuided) * specGuided.length : 0;
    const topSavingTools = confidenceLevel !== 'insufficient' ? computeTopSavingTools(specGuided, unstructured) : [];
    const monthlyTrend = computeMonthlyTrend(specGuided, unstructured);
    return {
        period,
        totalEntries: entries.length,
        specGuidedEntries: specGuided.length,
        unstructuredEntries: unstructured.length,
        avgTokensSpecGuided: avgSpecGuided,
        avgTokensUnstructured: avgUnstructured,
        savingsPct,
        estimatedSavingsTotal,
        confidenceLevel,
        topSavingTools,
        monthlyTrend,
    };
}
//# sourceMappingURL=token-savings-calculator.js.map