"use strict";
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2024-2026 Gracker (Chris)
// This file is part of SmartPerfetto. See LICENSE for details.
Object.defineProperty(exports, "__esModule", { value: true });
exports.SkillEventCollector = void 0;
exports.createEventCollector = createEventCollector;
// =============================================================================
// 事件收集器
// =============================================================================
class SkillEventCollector {
    constructor() {
        this.events = [];
        this.skillId = '';
        this.totalSteps = 0;
    }
    /**
     * 开始收集新的 skill 执行事件
     */
    start(skillId, totalSteps = 0) {
        this.events = [];
        this.skillId = skillId;
        this.totalSteps = totalSteps;
    }
    /**
     * 添加事件
     */
    addEvent(event) {
        this.events.push(event);
    }
    /**
     * 创建事件处理器（用于注入到 executor）
     */
    createHandler() {
        return (event) => {
            this.addEvent(event);
        };
    }
    /**
     * 获取所有事件
     */
    getEvents() {
        return [...this.events];
    }
    /**
     * 获取事件摘要
     */
    getSummary() {
        const eventCounts = {
            skill_started: 0,
            step_started: 0,
            step_completed: 0,
            display_result: 0,
            diagnostic_found: 0,
            ai_thinking: 0,
            ai_response: 0,
            skill_completed: 0,
            skill_error: 0,
        };
        let completedSteps = 0;
        let failedSteps = 0;
        let aiCallCount = 0;
        for (const event of this.events) {
            eventCounts[event.type]++;
            if (event.type === 'step_completed') {
                if (event.data?.success) {
                    completedSteps++;
                }
                else {
                    failedSteps++;
                }
            }
            if (event.type === 'ai_thinking') {
                aiCallCount++;
            }
        }
        const startTime = this.events.length > 0 ? this.events[0].timestamp : Date.now();
        const endTime = this.events.length > 0 ? this.events[this.events.length - 1].timestamp : Date.now();
        return {
            totalEvents: this.events.length,
            startTime,
            endTime,
            totalDurationMs: endTime - startTime,
            eventCounts,
            completedSteps,
            failedSteps,
            hasAICall: aiCallCount > 0,
            aiCallCount,
        };
    }
    /**
     * 获取当前进度信息
     */
    getProgress() {
        if (this.events.length === 0) {
            return {
                phase: 'initializing',
                progressPercent: 0,
                message: '初始化中...',
            };
        }
        const lastEvent = this.events[this.events.length - 1];
        // 检查是否完成
        if (lastEvent.type === 'skill_completed') {
            return {
                phase: 'completed',
                progressPercent: 100,
                message: '分析完成',
            };
        }
        if (lastEvent.type === 'skill_error') {
            return {
                phase: 'failed',
                progressPercent: 100,
                message: `分析失败: ${lastEvent.data?.error || '未知错误'}`,
            };
        }
        // 检查是否正在 AI 处理
        if (lastEvent.type === 'ai_thinking') {
            return {
                phase: 'ai_processing',
                currentStepId: lastEvent.stepId,
                progressPercent: this.calculateProgress(),
                message: 'AI 正在分析...',
            };
        }
        // 正在执行步骤
        const currentStep = this.findCurrentStep();
        return {
            phase: 'executing',
            currentStepId: currentStep?.stepId,
            currentStepName: currentStep?.data?.stepName,
            progressPercent: this.calculateProgress(),
            message: currentStep ? `正在执行: ${currentStep.stepId}` : '执行中...',
        };
    }
    /**
     * 计算进度百分比
     */
    calculateProgress() {
        if (this.totalSteps === 0) {
            // 如果不知道总步骤数，基于事件类型估算
            const completed = this.events.filter(e => e.type === 'step_completed').length;
            const started = this.events.filter(e => e.type === 'step_started').length;
            if (started === 0)
                return 0;
            return Math.min(95, Math.round((completed / started) * 100));
        }
        const completed = this.events.filter(e => e.type === 'step_completed').length;
        return Math.min(99, Math.round((completed / this.totalSteps) * 100));
    }
    /**
     * 找到当前正在执行的步骤
     */
    findCurrentStep() {
        // 找到最后一个 step_started 事件，且没有对应的 step_completed
        const startedSteps = new Set();
        const completedSteps = new Set();
        for (const event of this.events) {
            if (event.type === 'step_started' && event.stepId) {
                startedSteps.add(event.stepId);
            }
            if (event.type === 'step_completed' && event.stepId) {
                completedSteps.add(event.stepId);
            }
        }
        // 找到还没完成的步骤
        for (let i = this.events.length - 1; i >= 0; i--) {
            const event = this.events[i];
            if (event.type === 'step_started' && event.stepId && !completedSteps.has(event.stepId)) {
                return event;
            }
        }
        return undefined;
    }
    /**
     * 获取步骤时间线
     */
    getTimeline() {
        const timeline = [];
        const stepMap = new Map();
        for (const event of this.events) {
            if (!event.stepId)
                continue;
            if (event.type === 'step_started') {
                stepMap.set(event.stepId, {
                    stepId: event.stepId,
                    stepType: event.data?.stepType || 'unknown',
                    startTime: event.timestamp,
                    hasAI: false,
                });
            }
            if (event.type === 'step_completed') {
                const step = stepMap.get(event.stepId);
                if (step) {
                    step.endTime = event.timestamp;
                    step.durationMs = event.timestamp - step.startTime;
                    step.success = event.data?.success;
                }
            }
            if (event.type === 'ai_thinking' || event.type === 'ai_response') {
                const step = stepMap.get(event.stepId);
                if (step) {
                    step.hasAI = true;
                }
            }
        }
        return Array.from(stepMap.values());
    }
    /**
     * 清空收集器
     */
    clear() {
        this.events = [];
        this.skillId = '';
        this.totalSteps = 0;
    }
}
exports.SkillEventCollector = SkillEventCollector;
// =============================================================================
// 单例导出
// =============================================================================
function createEventCollector() {
    return new SkillEventCollector();
}
//# sourceMappingURL=eventCollector.js.map