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;
}
/**
 * 路由监控服务
 * 提供路由访问统计、性能监控、错误监控等功能
 */ import { getMonitoring } from "../../../utils/monitoring";
import { logger } from "../../../utils/logger";
/**
 * 路由监控服务
 */ export class RouterMonitoring {
    /**
   * 记录路由访问
   */ trackRouteAccess(to, from, success = true, error) {
        if (!this.config.enabled || !this.config.trackAccess) {
            return;
        }
        // 采样检查
        if (Math.random() > this.config.sampleRate) {
            return;
        }
        const record = {
            path: to.path,
            name: to.meta?.name,
            timestamp: Date.now(),
            from: from?.path,
            success,
            error: error?.message
        };
        // 如果有当前路由，计算持续时间
        if (this.currentRoute && this.currentRouteStartTime > 0) {
            record.duration = Date.now() - this.currentRouteStartTime;
        }
        this.accessRecords.push(record);
        // 限制记录数量
        if (this.accessRecords.length > this.config.maxRecords) {
            this.accessRecords.shift();
        }
        // 上报到监控服务
        this.monitoringService.trackEvent('route_access', {
            path: to.path,
            name: to.meta?.name,
            success,
            error: error?.message
        });
        // 更新当前路由
        this.currentRoute = to;
        this.currentRouteStartTime = Date.now();
        logger.debug('路由访问已记录', record);
    }
    /**
   * 记录路由性能指标
   */ trackRoutePerformance(routeName, metrics) {
        if (!this.config.enabled || !this.config.trackPerformance) {
            return;
        }
        // 采样检查
        if (Math.random() > this.config.sampleRate) {
            return;
        }
        const routeMetrics = this.performanceMetrics.get(routeName) || [];
        routeMetrics.push(metrics);
        // 限制记录数量
        if (routeMetrics.length > 100) {
            routeMetrics.shift();
        }
        this.performanceMetrics.set(routeName, routeMetrics);
        // 上报到监控服务
        this.monitoringService.reportPerformance({
            [`route_${routeName}_match`]: metrics.matchTime || 0,
            [`route_${routeName}_load`]: metrics.loadTime || 0,
            [`route_${routeName}_render`]: metrics.renderTime || 0,
            [`route_${routeName}_total`]: metrics.totalTime || 0
        });
        logger.debug(`路由性能指标已记录: ${routeName}`, metrics);
    }
    /**
   * 记录路由错误
   */ trackRouteError(error, context) {
        if (!this.config.enabled || !this.config.trackErrors) {
            return;
        }
        // 采样检查
        if (Math.random() > this.config.sampleRate) {
            return;
        }
        // 上报到监控服务
        this.monitoringService.captureError(error, {
            type: 'router',
            ...context
        });
        logger.error('路由错误已记录', error, context);
    }
    /**
   * 获取路由访问统计
   */ getAccessStats() {
        const stats = {
            total: this.accessRecords.length,
            success: 0,
            failed: 0,
            byPath: {},
            byName: {},
            averageDuration: 0
        };
        let totalDuration = 0;
        let durationCount = 0;
        for (const record of this.accessRecords){
            if (record.success) {
                stats.success++;
            } else {
                stats.failed++;
            }
            stats.byPath[record.path] = (stats.byPath[record.path] || 0) + 1;
            if (record.name) {
                stats.byName[record.name] = (stats.byName[record.name] || 0) + 1;
            }
            if (record.duration) {
                totalDuration += record.duration;
                durationCount++;
            }
        }
        stats.averageDuration = durationCount > 0 ? totalDuration / durationCount : 0;
        return stats;
    }
    /**
   * 获取路由性能统计
   */ getPerformanceStats() {
        const stats = {};
        for (const [routeName, metrics] of this.performanceMetrics.entries()){
            let totalMatchTime = 0;
            let totalLoadTime = 0;
            let totalRenderTime = 0;
            let totalTotalTime = 0;
            let count = 0;
            for (const metric of metrics){
                if (metric.matchTime) {
                    totalMatchTime += metric.matchTime;
                }
                if (metric.loadTime) {
                    totalLoadTime += metric.loadTime;
                }
                if (metric.renderTime) {
                    totalRenderTime += metric.renderTime;
                }
                if (metric.totalTime) {
                    totalTotalTime += metric.totalTime;
                }
                count++;
            }
            stats[routeName] = {
                count,
                averageMatchTime: count > 0 ? totalMatchTime / count : 0,
                averageLoadTime: count > 0 ? totalLoadTime / count : 0,
                averageRenderTime: count > 0 ? totalRenderTime / count : 0,
                averageTotalTime: count > 0 ? totalTotalTime / count : 0
            };
        }
        return stats;
    }
    /**
   * 清空所有记录
   */ clear() {
        this.accessRecords = [];
        this.performanceMetrics.clear();
        logger.debug('路由监控记录已清空');
    }
    /**
   * 获取监控报告
   */ getReport() {
        return {
            access: this.getAccessStats(),
            performance: this.getPerformanceStats(),
            timestamp: Date.now()
        };
    }
    constructor(config = {}){
        _define_property(this, "accessRecords", []);
        _define_property(this, "performanceMetrics", new Map());
        _define_property(this, "config", void 0);
        _define_property(this, "monitoringService", getMonitoring());
        _define_property(this, "currentRouteStartTime", 0);
        _define_property(this, "currentRoute", void 0);
        this.config = {
            enabled: config.enabled ?? true,
            trackAccess: config.trackAccess ?? true,
            trackPerformance: config.trackPerformance ?? true,
            trackErrors: config.trackErrors ?? true,
            maxRecords: config.maxRecords ?? 1000,
            sampleRate: config.sampleRate ?? 1.0
        };
    }
}
/**
 * 获取路由监控服务单例
 */ let routerMonitoringInstance = null;
export function getRouterMonitoring(config) {
    if (!routerMonitoringInstance) {
        routerMonitoringInstance = new RouterMonitoring(config);
    }
    return routerMonitoringInstance;
}

//# sourceMappingURL=RouterMonitoring.js.map