/**
 * event-mapper.ts — Maps DispatchEvents to nexusEventBus topics.
 *
 * Keeps the dispatch engine decoupled from the event bus wiring.
 * Each event kind gets a corresponding bus topic under the `dispatch.*` namespace.
 */
import { nexusEventBus } from '../event-bus.js';
export function emitDispatchEvent(runId, operativeId, event) {
    const base = { runId, operativeId };
    switch (event.kind) {
        case 'progress':
            nexusEventBus.emit('dispatch.event', {
                ...base, kind: 'progress',
                step: event.step, total: event.total, message: event.message,
            });
            break;
        case 'token-usage':
            nexusEventBus.emit('dispatch.event', {
                ...base, kind: 'token-usage',
                prompt: event.prompt, completion: event.completion,
            });
            nexusEventBus.emit('dispatch.token-usage', {
                ...base, prompt: event.prompt, completion: event.completion,
            });
            break;
        case 'file-change':
            nexusEventBus.emit('dispatch.event', {
                ...base, kind: 'file-change', path: event.path,
            });
            break;
        case 'tool-call':
            nexusEventBus.emit('dispatch.event', {
                ...base, kind: 'tool-call', toolName: event.toolName,
            });
            break;
        case 'done':
            nexusEventBus.emit('dispatch.event', {
                ...base, kind: 'done', exitCode: event.exitCode,
            });
            break;
        case 'error':
            nexusEventBus.emit('dispatch.event', {
                ...base, kind: 'error', error: event.error,
            });
            nexusEventBus.emit('dispatch.failed', { ...base, error: event.error });
            break;
        // stdout/stderr/tool-result: don't flood the bus — dashboard streams these
        // directly from the in-process SSE push via the handle's event iterator.
        default:
            break;
    }
}
export function emitDispatchComplete(runId, operativeId, result) {
    nexusEventBus.emit('dispatch.complete', {
        runId,
        operativeId,
        success: result.success,
        tokensUsed: result.tokensUsed.prompt + result.tokensUsed.completion,
        costUsd: result.costUsd,
        filesChanged: result.filesChanged.length,
        durationMs: result.durationMs,
        summary: result.summary,
    });
}
