/**
 * Route Analytics Module
 *
 * Route statistics and hit tracking extracted from VirtualRouterEngine.
 */
export class RouteAnalytics {
    routeStats = new Map();
    incrementRouteStat(routeName, providerKey, hitRecord) {
        const nextLastHit = {
            timestampMs: hitRecord.timestampMs,
            ...(hitRecord.hitReason ? { reason: hitRecord.hitReason } : {}),
            ...(typeof hitRecord.requestTokens === 'number' ? { requestTokens: hitRecord.requestTokens } : {}),
            ...(typeof hitRecord.selectionPenalty === 'number' ? { selectionPenalty: hitRecord.selectionPenalty } : {}),
            stopMessageActive: hitRecord.stopMessage.active,
            ...(hitRecord.stopMessage.mode !== 'unset' ? { stopMessageMode: hitRecord.stopMessage.mode } : {}),
            ...(hitRecord.stopMessage.remaining >= 0 ? { stopMessageRemaining: hitRecord.stopMessage.remaining } : {})
        };
        if (!this.routeStats.has(routeName)) {
            this.routeStats.set(routeName, { hits: 1, lastProvider: providerKey, lastHit: nextLastHit });
            return;
        }
        const stats = this.routeStats.get(routeName);
        stats.hits += 1;
        stats.lastProvider = providerKey;
        stats.lastHit = nextLastHit;
    }
    getRouteStats(routeName) {
        return this.routeStats.get(routeName);
    }
    getAllRouteStats() {
        return this.routeStats;
    }
    extractExcludedProviderKeySet(metadata) {
        if (!metadata)
            return new Set();
        const raw = metadata.excludedProviderKeys;
        if (!Array.isArray(raw) || raw.length === 0)
            return new Set();
        const normalized = raw
            .map((value) => (typeof value === 'string' ? value.trim() : ''))
            .filter((value) => Boolean(value));
        return new Set(normalized);
    }
}
