/**
 * Tool emitters — typed emission wrappers for ToolEvent domain.
 */
import { createEventEnvelope } from '../events/envelope.js';
/** Emit TOOL_RECEIVED when a tool call is received from the LLM. */
export function emitToolReceived(bus, ctx, data) {
    bus.emit('tools', createEventEnvelope('TOOL_RECEIVED', { type: 'TOOL_RECEIVED', ...data }, ctx));
}
/** Emit TOOL_VALIDATED when tool arguments pass schema validation. */
export function emitToolValidated(bus, ctx, data) {
    bus.emit('tools', createEventEnvelope('TOOL_VALIDATED', { type: 'TOOL_VALIDATED', ...data }, ctx));
}
/** Emit TOOL_PREHOOKED after pre-execution hooks have run. */
export function emitToolPrehooked(bus, ctx, data) {
    bus.emit('tools', createEventEnvelope('TOOL_PREHOOKED', { type: 'TOOL_PREHOOKED', ...data }, ctx));
}
/** Emit TOOL_PERMISSIONED after permission check completes. */
export function emitToolPermissioned(bus, ctx, data) {
    bus.emit('tools', createEventEnvelope('TOOL_PERMISSIONED', { type: 'TOOL_PERMISSIONED', ...data }, ctx));
}
/** Emit TOOL_EXECUTING when the tool starts executing. */
export function emitToolExecuting(bus, ctx, data) {
    bus.emit('tools', createEventEnvelope('TOOL_EXECUTING', { type: 'TOOL_EXECUTING', ...data }, ctx));
}
/** Emit TOOL_MAPPED after the result is mapped for the provider. */
export function emitToolMapped(bus, ctx, data) {
    bus.emit('tools', createEventEnvelope('TOOL_MAPPED', { type: 'TOOL_MAPPED', ...data }, ctx));
}
/** Emit TOOL_POSTHOOKED after post-execution hooks have run. */
export function emitToolPosthooked(bus, ctx, data) {
    bus.emit('tools', createEventEnvelope('TOOL_POSTHOOKED', { type: 'TOOL_POSTHOOKED', ...data }, ctx));
}
/**
 * Convert any tool result (ToolResult, SyntheticToolResult, etc.) to a ToolResultSummary
 * for safe emission in the event stream. Never leaks raw output; provides kind, byteSize, preview.
 */
export function toToolResultSummary(result) {
    if (!result.success) {
        const errStr = result.error ?? 'unknown error';
        return {
            kind: 'error',
            byteSize: errStr.length,
            preview: errStr.slice(0, 100),
        };
    }
    const output = result.output ?? '';
    let kind;
    let preview;
    try {
        JSON.parse(output);
        kind = 'json';
        preview = output.slice(0, 100);
    }
    catch {
        kind = 'text';
        preview = output.slice(0, 100);
    }
    return { kind, byteSize: output.length, preview };
}
/** Emit TOOL_SUCCEEDED when a tool call completes successfully. */
export function emitToolSucceeded(bus, ctx, data) {
    bus.emit('tools', createEventEnvelope('TOOL_SUCCEEDED', { type: 'TOOL_SUCCEEDED', ...data }, ctx));
}
/** Emit TOOL_FAILED when a tool call fails. */
export function emitToolFailed(bus, ctx, data) {
    bus.emit('tools', createEventEnvelope('TOOL_FAILED', { type: 'TOOL_FAILED', ...data }, ctx));
}
/** Emit TOOL_RECONCILED when unresolved tool calls are synthesized. */
export function emitToolReconciled(bus, ctx, data) {
    bus.emit('tools', createEventEnvelope('TOOL_RECONCILED', { type: 'TOOL_RECONCILED', ...data }, ctx));
}
/** Emit TOOL_CANCELLED when a tool call is cancelled. */
export function emitToolCancelled(bus, ctx, data) {
    bus.emit('tools', createEventEnvelope('TOOL_CANCELLED', { type: 'TOOL_CANCELLED', ...data }, ctx));
}
/** Emit BUDGET_EXCEEDED_MS when wall-clock time limit is breached. */
export function emitBudgetExceededMs(bus, ctx, data) {
    bus.emit('tools', createEventEnvelope('BUDGET_EXCEEDED_MS', { type: 'BUDGET_EXCEEDED_MS', ...data }, ctx));
}
/** Emit BUDGET_EXCEEDED_TOKENS when token consumption limit is breached. */
export function emitBudgetExceededTokens(bus, ctx, data) {
    bus.emit('tools', createEventEnvelope('BUDGET_EXCEEDED_TOKENS', { type: 'BUDGET_EXCEEDED_TOKENS', ...data }, ctx));
}
/** Emit BUDGET_EXCEEDED_COST when cost-in-USD limit is breached. */
export function emitBudgetExceededCost(bus, ctx, data) {
    bus.emit('tools', createEventEnvelope('BUDGET_EXCEEDED_COST', { type: 'BUDGET_EXCEEDED_COST', ...data }, ctx));
}
