"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.recordOutcome = recordOutcome;
exports.getPatternOutcomeStats = getPatternOutcomeStats;
exports.getAllPatternOutcomeStats = getAllPatternOutcomeStats;
exports.getPathSuccessDistribution = getPathSuccessDistribution;
exports.getOutcomeHistory = getOutcomeHistory;
const workflow_tracker_1 = require("./workflow-tracker");
const outcomeHistory = [];
const MAX_OUTCOMES = 2000;
function recordOutcome(pattern, action, workflowSessionId, workflowSuccess, position) {
    const record = {
        patternId: pattern.patternId,
        description: pattern.description,
        from: pattern.from,
        to: pattern.to,
        action,
        timestamp: Date.now(),
        workflowSessionId,
        workflowSuccess,
        position,
    };
    outcomeHistory.push(record);
    if (outcomeHistory.length > MAX_OUTCOMES) {
        outcomeHistory.splice(0, outcomeHistory.length - MAX_OUTCOMES);
    }
    return record;
}
function getPatternOutcomeStats(patternId) {
    const records = outcomeHistory.filter((r) => r.patternId === patternId);
    if (records.length === 0)
        return null;
    const acceptCount = records.filter((r) => r.action === 'accept').length;
    const rejectCount = records.filter((r) => r.action === 'reject').length;
    const editDiffCount = records.filter((r) => r.action === 'edit_diff').length;
    const retryCount = records.filter((r) => r.action === 'retry').length;
    const total = records.length;
    const workflowSuccessCount = records.filter((r) => r.workflowSuccess).length;
    return {
        patternId,
        description: records[0].description,
        totalAppearances: total,
        acceptCount,
        rejectCount,
        editDiffCount,
        retryCount,
        acceptanceRate: total > 0 ? acceptCount / total : 0,
        workflowSuccessRate: total > 0 ? workflowSuccessCount / total : 0,
    };
}
function getAllPatternOutcomeStats() {
    const grouped = new Map();
    for (const record of outcomeHistory) {
        if (!grouped.has(record.patternId)) {
            grouped.set(record.patternId, {
                patternId: record.patternId,
                description: record.description,
                totalAppearances: 0,
                acceptCount: 0,
                rejectCount: 0,
                editDiffCount: 0,
                retryCount: 0,
                acceptanceRate: 0,
                workflowSuccessRate: 0,
            });
        }
        const stats = grouped.get(record.patternId);
        stats.totalAppearances++;
        if (record.action === 'accept')
            stats.acceptCount++;
        else if (record.action === 'reject')
            stats.rejectCount++;
        else if (record.action === 'edit_diff')
            stats.editDiffCount++;
        else if (record.action === 'retry')
            stats.retryCount++;
    }
    return Array.from(grouped.values()).map((stats) => ({
        ...stats,
        acceptanceRate: stats.totalAppearances > 0 ? stats.acceptCount / stats.totalAppearances : 0,
        workflowSuccessRate: stats.totalAppearances > 0
            ? outcomeHistory.filter((r) => r.patternId === stats.patternId && r.workflowSuccess).length / stats.totalAppearances
            : 0,
    }));
}
function getPathSuccessDistribution() {
    const workflows = (0, workflow_tracker_1.getCompletedWorkflows)(100);
    const byLength = new Map();
    for (const wf of workflows) {
        const len = wf.steps.length;
        if (!byLength.has(len))
            byLength.set(len, { total: 0, successful: 0 });
        const entry = byLength.get(len);
        entry.total++;
        if (wf.success)
            entry.successful++;
    }
    return Array.from(byLength.entries())
        .sort((a, b) => a[0] - b[0])
        .map(([pathLength, data]) => ({
        pathLength,
        count: data.total,
        successRate: data.total > 0 ? data.successful / data.total : 0,
    }));
}
function getOutcomeHistory(limit = 50) {
    return outcomeHistory.slice(-limit).reverse();
}
//# sourceMappingURL=outcome-recorder.js.map