"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.recordTimeSaved = recordTimeSaved;
exports.getTimeSavedStats = getTimeSavedStats;
exports.getTimeSavedByPattern = getTimeSavedByPattern;
exports.clearTimeSaved = clearTimeSaved;
const records = [];
const MAX_RECORDS = 10000;
function estimateTimeSeconds(from, to) {
    const fromLines = from.split('\n').length;
    const toLines = to.split('\n').length;
    const maxLines = Math.max(fromLines, toLines);
    if (maxLines <= 1)
        return 15;
    if (maxLines <= 5)
        return 45;
    if (maxLines <= 15)
        return 120;
    return 300;
}
function recordTimeSaved(data) {
    const rec = {
        patternId: data.patternId,
        category: data.category || 'uncategorized',
        description: data.description || '',
        from: data.from,
        to: data.to,
        language: data.language || 'unknown',
        estimatedTimeSeconds: estimateTimeSeconds(data.from, data.to),
        timestamp: Date.now(),
    };
    records.push(rec);
    if (records.length > MAX_RECORDS)
        records.splice(0, records.length - MAX_RECORDS);
    return rec;
}
function getTimeSavedStats() {
    const totalTimeSavedSeconds = records.reduce((s, r) => s + r.estimatedTimeSeconds, 0);
    const catMap = new Map();
    const langMap = new Map();
    const patternMap = new Map();
    for (const r of records) {
        const c = catMap.get(r.category) || { totalSeconds: 0, count: 0 };
        c.totalSeconds += r.estimatedTimeSeconds;
        c.count++;
        catMap.set(r.category, c);
        const l = langMap.get(r.language) || { totalSeconds: 0, count: 0 };
        l.totalSeconds += r.estimatedTimeSeconds;
        l.count++;
        langMap.set(r.language, l);
        const p = patternMap.get(r.patternId) || { description: r.description, totalSeconds: 0, count: 0 };
        p.totalSeconds += r.estimatedTimeSeconds;
        p.count++;
        patternMap.set(r.patternId, p);
    }
    const now = new Date();
    const sevenDaysAgo = now.getTime() - 7 * 86400000;
    const dayMap = new Map();
    for (const r of records) {
        if (r.timestamp < sevenDaysAgo)
            continue;
        const d = new Date(r.timestamp).toISOString().slice(0, 10);
        const e = dayMap.get(d) || { totalSeconds: 0, count: 0 };
        e.totalSeconds += r.estimatedTimeSeconds;
        e.count++;
        dayMap.set(d, e);
    }
    return {
        totalTimeSavedSeconds,
        totalAcceptances: records.length,
        perCategory: Array.from(catMap.entries())
            .map(([k, v]) => ({ category: k, totalSeconds: v.totalSeconds, count: v.count }))
            .sort((a, b) => b.totalSeconds - a.totalSeconds),
        perLanguage: Array.from(langMap.entries())
            .map(([k, v]) => ({ language: k, totalSeconds: v.totalSeconds, count: v.count }))
            .sort((a, b) => b.totalSeconds - a.totalSeconds),
        topPatterns: Array.from(patternMap.entries())
            .map(([k, v]) => ({ patternId: k, description: v.description, totalSeconds: v.totalSeconds, count: v.count }))
            .sort((a, b) => b.totalSeconds - a.totalSeconds)
            .slice(0, 20),
        dailyTrend: Array.from(dayMap.entries())
            .map(([k, v]) => ({ date: k, totalSeconds: v.totalSeconds, count: v.count }))
            .sort((a, b) => a.date.localeCompare(b.date)),
    };
}
function getTimeSavedByPattern(patternId) {
    const matched = records.filter((r) => r.patternId === patternId);
    return {
        totalSeconds: matched.reduce((s, r) => s + r.estimatedTimeSeconds, 0),
        count: matched.length,
        records: matched.slice(-50),
    };
}
function clearTimeSaved() {
    records.length = 0;
}
//# sourceMappingURL=time-saved.js.map