import { emitTransportConnected, emitTransportDisconnected, emitTransportInitializing, emitTransportTerminalFailure, } from '../runtime/emitters/index.js';
import { summarizeError } from '../utils/error-display.js';
import { logger } from '../utils/logger.js';
export class DaemonTransportEventsHelper {
    context;
    constructor(context) {
        this.context = context;
    }
    transportId() {
        return `daemon:http:${this.context.host}:${this.context.port}`;
    }
    transportScheme() {
        return this.context.tlsState()?.scheme ?? 'http';
    }
    transportEndpoint() {
        return `${this.transportScheme()}://${this.context.host}:${this.context.port}`;
    }
    emitterContext() {
        return {
            sessionId: 'daemon-server',
            traceId: `daemon-server:${this.context.host}:${this.context.port}`,
            source: 'daemon-server',
        };
    }
    emitTransportInitializing() {
        if (!this.context.runtimeBus)
            return;
        emitTransportInitializing(this.context.runtimeBus, this.emitterContext(), {
            transportId: this.transportId(),
            protocol: 'http-daemon',
        });
        void this.fireTransportHook('initializing', {
            transportId: this.transportId(),
            protocol: 'http-daemon',
        });
    }
    emitTransportConnected() {
        if (!this.context.runtimeBus)
            return;
        emitTransportConnected(this.context.runtimeBus, this.emitterContext(), {
            transportId: this.transportId(),
            endpoint: this.transportEndpoint(),
        });
        void this.fireTransportHook('connected', {
            transportId: this.transportId(),
            endpoint: this.transportEndpoint(),
        });
    }
    emitTransportDisconnected(reason, willRetry) {
        if (!this.context.runtimeBus)
            return;
        emitTransportDisconnected(this.context.runtimeBus, this.emitterContext(), {
            transportId: this.transportId(),
            reason,
            willRetry,
        });
        void this.fireTransportHook('disconnected', {
            transportId: this.transportId(),
            reason,
            willRetry,
        });
    }
    emitTransportTerminalFailure(error) {
        if (!this.context.runtimeBus)
            return;
        emitTransportTerminalFailure(this.context.runtimeBus, this.emitterContext(), {
            transportId: this.transportId(),
            error,
        });
        void this.fireTransportHook('failed', {
            transportId: this.transportId(),
            error,
        });
    }
    async fireTransportHook(specific, payload) {
        if (!this.context.hookDispatcher)
            return;
        try {
            await this.context.hookDispatcher.fire({
                path: `Lifecycle:transport:${specific}`,
                phase: 'Lifecycle',
                category: 'transport',
                specific,
                sessionId: 'daemon-server',
                timestamp: Date.now(),
                payload,
            });
        }
        catch (error) {
            logger.warn('[daemon-transport] lifecycle hook failed', {
                specific,
                error: summarizeError(error),
            });
        }
    }
}
