export async function renderBuiltinChannelEvent(context, surface, request) {
    const router = context.deps.deliveryRouter;
    const binding = request.routeId ? context.deps.routeBindings.getBinding(request.routeId) : undefined;
    const responseId = await router.deliver({
        target: buildDeliveryTarget(surface, request, binding),
        body: request.text,
        title: request.title,
        jobId: binding?.jobId ?? request.routeId ?? `channel:${surface}`,
        runId: binding?.runId ?? request.agentId ?? request.sessionId ?? `${surface}:${Date.now()}`,
        ...(request.agentId ? { agentId: request.agentId } : {}),
        ...(request.sessionId ? { sessionId: request.sessionId } : {}),
        status: renderStatus(request),
        includeLinks: request.phase !== 'progress',
        ...(binding ? { binding: toDeliveryRouteBinding(binding) } : {}),
        metadata: {
            phase: request.phase,
            pending: request.pending ?? {},
            renderMetadata: request.metadata,
            eventKinds: request.events.map((event) => event.kind),
        },
    });
    return {
        delivered: true,
        ...(responseId ? { responseId } : {}),
        ...(binding?.threadId ? { threadId: binding.threadId } : {}),
        metadata: {
            surface,
            phase: request.phase,
            via: 'channel-delivery-router',
        },
    };
}
export async function notifyBuiltinApprovalViaRouter(context, surface, approval, binding) {
    const router = context.deps.deliveryRouter;
    const status = approval.status === 'approved'
        ? 'completed'
        : approval.status === 'denied' || approval.status === 'cancelled' || approval.status === 'expired'
            ? 'failed'
            : 'running';
    await router.deliver({
        target: buildDeliveryTarget(surface, { pending: {} }, binding),
        body: formatApprovalMessage(approval),
        title: `Approval ${approval.status}: ${approval.request.tool}`,
        jobId: binding.jobId ?? `approval:${approval.id}`,
        runId: binding.runId ?? approval.id,
        status,
        includeLinks: true,
        binding: toDeliveryRouteBinding(binding),
    });
}
export function listBuiltinAgentTools(context, surface) {
    return context.listTools(surface).map((descriptor) => ({
        definition: {
            name: descriptor.name,
            description: descriptor.description,
            parameters: descriptor.inputSchema ?? {
                type: 'object',
                additionalProperties: true,
            },
            sideEffects: ['network', 'state'],
            concurrency: 'serial',
        },
        execute: async (args) => {
            const result = await context.runTool(surface, descriptor.id, args);
            if (result === null) {
                return {
                    success: false,
                    error: `Unknown channel tool '${descriptor.id}' for surface '${surface}'.`,
                };
            }
            return {
                success: true,
                output: JSON.stringify({ surface, toolId: descriptor.id, result }, null, 2),
            };
        },
    }));
}
function buildDeliveryTarget(surface, request, binding) {
    const pending = request.pending ?? {};
    const address = surface === 'webhook'
        ? readPendingString(pending, 'callbackUrl')
            ?? readMetadataString(binding?.metadata, 'callbackUrl')
        : readPendingString(pending, 'targetAddress')
            ?? readPendingString(pending, 'responseUrl')
            ?? readPendingString(pending, 'channelId')
            ?? readPendingString(pending, 'topic')
            ?? binding?.channelId
            ?? binding?.externalId;
    if (surface === 'webhook') {
        return {
            kind: 'webhook',
            surfaceKind: 'webhook',
            ...(address ? { address } : {}),
        };
    }
    return {
        kind: 'surface',
        surfaceKind: surface,
        ...(address ? { address } : {}),
    };
}
function toDeliveryRouteBinding(binding) {
    return {
        id: binding.id,
        surfaceKind: binding.surfaceKind,
        surfaceId: binding.surfaceId,
        externalId: binding.externalId,
        ...(binding.threadId ? { threadId: binding.threadId } : {}),
        ...(binding.channelId ? { channelId: binding.channelId } : {}),
        ...(binding.title ? { title: binding.title } : {}),
        metadata: { ...binding.metadata },
    };
}
function renderStatus(request) {
    if (request.events.some((event) => event.kind === 'error'))
        return 'failed';
    if (request.phase === 'final')
        return 'completed';
    if (request.phase === 'approval')
        return 'running';
    return 'running';
}
function formatApprovalMessage(approval) {
    const lines = [
        `Approval ${approval.status}: ${approval.id}`,
        `Tool: ${approval.request.tool}`,
        approval.request.analysis.summary,
        approval.request.analysis.target ? `Target: ${approval.request.analysis.target}` : '',
        approval.resolvedBy ? `Resolved by: ${approval.resolvedBy}` : '',
    ].filter((line) => line.trim().length > 0);
    return lines.join('\n');
}
function readPendingString(pending, key) {
    const value = pending[key];
    return typeof value === 'string' && value.trim().length > 0 ? value.trim() : undefined;
}
function readMetadataString(metadata, key) {
    if (!metadata)
        return undefined;
    const value = metadata[key];
    return typeof value === 'string' && value.trim().length > 0 ? value.trim() : undefined;
}
