/**
 * Stream lifecycle diagnostics for provider clients.
 *
 * Tracks connection, iteration, event counts, and completion
 * to fill the observability gap between stream start and timeout/error.
 * All output is debug-level only.
 */
import { createLogger } from './debug.js';
export function createStreamDiagnostics(component, context) {
    const log = createLogger(component);
    const startTime = Date.now();
    let eventCount = 0;
    let lastEventType = '';
    let lastEventTime = 0;
    let connected = false;
    let firstEventLogged = false;
    return {
        onConnected() {
            connected = true;
            log.debug('Stream connected', { ...context, elapsedMs: Date.now() - startTime });
        },
        onFirstEvent(eventType) {
            if (firstEventLogged)
                return;
            firstEventLogged = true;
            log.debug('Stream first event', { ...context, firstEventType: eventType, elapsedMs: Date.now() - startTime });
        },
        onEvent(eventType) {
            eventCount++;
            lastEventType = eventType;
            lastEventTime = Date.now();
        },
        onIdleTimeoutFired() {
            log.debug('Idle timeout fired', {
                ...context,
                eventCount,
                lastEventType,
                msSinceLastEvent: lastEventTime > 0 ? Date.now() - lastEventTime : undefined,
                connected,
                iterationStarted: firstEventLogged,
            });
        },
        onStreamError(eventType, message) {
            log.debug('Stream error event', { ...context, eventType, message, eventCount });
        },
        onCompleted(reason, detail) {
            log.debug('Stream completed', {
                ...context,
                reason,
                detail,
                eventCount,
                lastEventType,
                durationMs: Date.now() - startTime,
                connected,
                iterationStarted: firstEventLogged,
            });
        },
    };
}
//# sourceMappingURL=streamDiagnostics.js.map