/**
 * Zenith CLI Bridge
 *
 * The ONLY interface between CLI and plugins.
 *
 * ═══════════════════════════════════════════════════════════════════════════════
 * CLI BRIDGE RULES (CANONICAL)
 * ═══════════════════════════════════════════════════════════════════════════════
 *
 * 1. No runtime emitters - plugins return data, CLI serializes blindly
 * 2. No plugin typing - all data is unknown
 * 3. No semantic helpers - CLI is blind to what data means
 *
 * The CLI dispatches hooks and collects returns. It never inspects payloads.
 * ═══════════════════════════════════════════════════════════════════════════════
 */
const hookRegistry = new Map();
/**
 * Register a hook handler
 *
 * @internal Called by CLIBridgeAPI.on()
 */
export function registerHook(hook, handler) {
    if (!hookRegistry.has(hook)) {
        hookRegistry.set(hook, []);
    }
    hookRegistry.get(hook).push(handler);
}
/**
 * Clear all registered hooks
 *
 * @internal Used for testing and cleanup
 */
export function clearHooks() {
    hookRegistry.clear();
}
// ============================================
// Hook Execution (CLI-facing)
// ============================================
/**
 * Run all handlers for a hook (fire-and-forget)
 *
 * CLI calls this for lifecycle events.
 * No return values are collected.
 *
 * @param hook - Hook name to dispatch
 * @param ctx - Hook context
 */
export async function runPluginHooks(hook, ctx) {
    const handlers = hookRegistry.get(hook) || [];
    for (const handler of handlers) {
        try {
            await handler(ctx);
        }
        catch (error) {
            console.error(`[Zenith] Hook "${hook}" error:`, error);
        }
    }
}
/**
 * Collect return values from all handlers for a hook
 *
 * CLI calls this for 'cli:runtime:collect' to gather plugin payloads.
 * Only RuntimePayload-shaped returns are collected.
 *
 * @param hook - Hook name to dispatch
 * @param ctx - Hook context
 * @returns Array of runtime payloads from plugins
 */
export async function collectHookReturns(hook, ctx) {
    const handlers = hookRegistry.get(hook) || [];
    const results = [];
    for (const handler of handlers) {
        try {
            const result = await handler(ctx);
            // Only collect properly shaped payloads
            if (result &&
                typeof result === 'object' &&
                'namespace' in result &&
                'payload' in result &&
                typeof result.namespace === 'string') {
                results.push(result);
            }
        }
        catch (error) {
            console.error(`[Zenith] Hook "${hook}" collection error:`, error);
        }
    }
    return results;
}
/**
 * Build runtime envelope from collected payloads
 *
 * CLI calls this to serialize plugin data for injection.
 * CLI never inspects the envelope contents.
 *
 * @param payloads - Array of runtime payloads from collectHookReturns
 * @returns Envelope object: { [namespace]: payload }
 */
export function buildRuntimeEnvelope(payloads) {
    const envelope = {};
    for (const { namespace, payload } of payloads) {
        envelope[namespace] = payload;
    }
    return envelope;
}
// ============================================
// Bridge API Factory
// ============================================
/**
 * Create a CLI Bridge API for plugin registration
 *
 * CLI calls this once and passes to each plugin's registerCLI method.
 *
 * @returns CLIBridgeAPI instance
 */
export function createBridgeAPI() {
    return {
        on: registerHook
    };
}
