"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.recordStep = recordStep;
exports.completeWorkflow = completeWorkflow;
exports.getActiveWorkflows = getActiveWorkflows;
exports.getCompletedWorkflows = getCompletedWorkflows;
exports.getWorkflowStats = getWorkflowStats;
exports.getCurrentSessionId = getCurrentSessionId;
const activeSessions = new Map();
const completedWorkflows = [];
const MAX_COMPLETED = 500;
const SESSION_TIMEOUT_MS = 30 * 60 * 1000;
const CLEANUP_INTERVAL_MS = 5 * 60 * 1000;
let currentSessionId = `wf_${Date.now()}`;
let lastActivity = Date.now();
setInterval(() => {
    const now = Date.now();
    for (const [sid, steps] of activeSessions) {
        if (steps.length === 0)
            continue;
        const lastStep = steps[steps.length - 1];
        if (now - lastStep.timestamp > SESSION_TIMEOUT_MS) {
            completeWorkflow(sid, false);
        }
    }
}, CLEANUP_INTERVAL_MS);
function recordStep(pattern, action) {
    const now = Date.now();
    if (now - lastActivity > SESSION_TIMEOUT_MS) {
        currentSessionId = `wf_${Date.now()}`;
    }
    lastActivity = now;
    if (!activeSessions.has(currentSessionId)) {
        activeSessions.set(currentSessionId, []);
    }
    const step = {
        action,
        patternId: pattern.patternId,
        description: pattern.description,
        from: pattern.from,
        to: pattern.to,
        timestamp: now,
        sessionId: currentSessionId,
    };
    activeSessions.get(currentSessionId).push(step);
    if (step.action === 'accept') {
        completeWorkflow(currentSessionId, true);
        currentSessionId = `wf_${Date.now()}`;
    }
    return step;
}
function completeWorkflow(sessionId, success) {
    const steps = activeSessions.get(sessionId);
    if (!steps || steps.length === 0)
        return null;
    const session = {
        sessionId,
        startedAt: steps[0].timestamp,
        completedAt: Date.now(),
        steps: [...steps],
        success,
        language: steps[0]?.from?.includes('typescript') ? 'typescript' : 'unknown',
        framework: 'unknown',
    };
    completedWorkflows.push(session);
    if (completedWorkflows.length > MAX_COMPLETED) {
        completedWorkflows.splice(0, completedWorkflows.length - MAX_COMPLETED);
    }
    activeSessions.delete(sessionId);
    return session;
}
function getActiveWorkflows() {
    const now = Date.now();
    return Array.from(activeSessions.entries())
        .filter(([_, steps]) => steps.length > 0)
        .map(([sessionId, steps]) => ({
        sessionId,
        stepCount: steps.length,
        duration: now - steps[0].timestamp,
    }));
}
function getCompletedWorkflows(limit = 20) {
    return completedWorkflows.slice(-limit).reverse();
}
function getWorkflowStats() {
    const total = completedWorkflows.length;
    const successful = completedWorkflows.filter((w) => w.success).length;
    const totalSteps = completedWorkflows.reduce((sum, w) => sum + w.steps.length, 0);
    return {
        totalCompleted: total,
        successRate: total > 0 ? successful / total : 0,
        avgStepsPerWorkflow: total > 0 ? totalSteps / total : 0,
        activeSessions: activeSessions.size,
    };
}
function getCurrentSessionId() {
    return currentSessionId;
}
//# sourceMappingURL=workflow-tracker.js.map