export class ProviderErrorCenter {
    listeners = new Set();
    subscribe(listener) {
        this.listeners.add(listener);
        return () => {
            this.listeners.delete(listener);
        };
    }
    emit(event) {
        const enriched = this.normalize(event);
        for (const listener of this.listeners) {
            try {
                listener(enriched);
            }
            catch {
                // Listener failures should not break propagation
            }
        }
        return enriched;
    }
    normalize(event) {
        const timestamp = typeof event.timestamp === 'number' ? event.timestamp : Date.now();
        const code = event.code?.toString() || 'ERR_UNKNOWN';
        const message = event.message || code;
        const stage = event.stage || 'unknown';
        const runtime = event.runtime || {};
        return {
            code,
            message,
            stage,
            status: event.status,
            recoverable: event.recoverable,
            runtime,
            timestamp,
            details: event.details
        };
    }
}
export const providerErrorCenter = new ProviderErrorCenter();
