/**
 * 性能追踪模块
 * 负责收集和上报启动性能指标
 */ "use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get PerformanceTracker () {
        return PerformanceTracker;
    },
    get STARTUP_METRIC_UNITS () {
        return STARTUP_METRIC_UNITS;
    }
});
function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
const STARTUP_METRIC_UNITS = {
    /**
   * 毫秒单位
   */ ms: 'ms',
    /**
   * 无单位（分数）
   */ none: ''
};
let PerformanceTracker = class PerformanceTracker {
    /**
   * 记录服务初始化耗时
   */ recordServiceInit(duration) {
        this.metrics.serviceInitDuration = duration;
    }
    /**
   * 记录初始化耗时
   */ recordInitialization(duration) {
        this.metrics.initializationDuration = duration;
    }
    /**
   * 记录首次渲染耗时
   */ recordFirstRender(duration) {
        this.metrics.firstRenderDuration = duration;
    }
    /**
   * 记录总启动耗时
   */ recordTotalStartup(duration) {
        this.metrics.totalStartupDuration = duration;
    }
    /**
   * 收集Web Vitals指标
   */ collectWebVitals() {
        if (typeof window === 'undefined') {
            return;
        }
        // 收集FCP（首次内容绘制）
        if ('PerformanceObserver' in window) {
            try {
                const fcpObserver = new PerformanceObserver((list)=>{
                    for (const entry of list.getEntries()){
                        if (entry.name === 'first-contentful-paint') {
                            this.metrics.fcp = entry.startTime;
                            this.report();
                        }
                    }
                });
                fcpObserver.observe({
                    entryTypes: [
                        'paint'
                    ]
                });
            } catch (e) {
            // 忽略错误
            }
        }
        // 收集LCP（最大内容绘制）
        if ('PerformanceObserver' in window) {
            try {
                const lcpObserver = new PerformanceObserver((list)=>{
                    const entries = list.getEntries();
                    const lastEntry = entries[entries.length - 1];
                    this.metrics.lcp = lastEntry.startTime;
                    this.report();
                });
                lcpObserver.observe({
                    entryTypes: [
                        'largest-contentful-paint'
                    ]
                });
            } catch (e) {
            // 忽略错误
            }
        }
        // 收集FID（首次输入延迟）
        if ('PerformanceObserver' in window) {
            try {
                const fidObserver = new PerformanceObserver((list)=>{
                    for (const entry of list.getEntries()){
                        // PerformanceEventTiming 类型检查
                        if ('processingStart' in entry && 'startTime' in entry) {
                            const eventEntry = entry;
                            this.metrics.fid = eventEntry.processingStart - eventEntry.startTime;
                            this.report();
                        }
                    }
                });
                fidObserver.observe({
                    entryTypes: [
                        'first-input'
                    ]
                });
            } catch (e) {
            // 忽略错误
            }
        }
        // 收集CLS（累积布局偏移）
        if ('PerformanceObserver' in window) {
            try {
                let clsValue = 0;
                const clsObserver = new PerformanceObserver((list)=>{
                    for (const entry of list.getEntries()){
                        if (!entry.hadRecentInput) {
                            clsValue += entry.value;
                        }
                    }
                    this.metrics.cls = clsValue;
                    this.report();
                });
                clsObserver.observe({
                    entryTypes: [
                        'layout-shift'
                    ]
                });
            } catch (e) {
            // 忽略错误
            }
        }
    }
    /**
   * 上报性能指标
   */ report() {
        if (this.monitoring.reportPerformance) {
            this.monitoring.reportPerformance(this.metrics);
        }
    }
    /**
   * 获取所有指标
   */ getMetrics() {
        return {
            ...this.metrics
        };
    }
    /**
   * 获取总启动耗时
   */ getTotalDuration() {
        return performance.now() - this.startTime;
    }
    constructor(monitoring){
        _define_property(this, "monitoring", void 0);
        _define_property(this, "metrics", {});
        _define_property(this, "startTime", void 0);
        this.monitoring = monitoring;
        this.startTime = performance.now();
    }
};

//# sourceMappingURL=performanceTracker.js.map