/**
 * Print mode (single-shot): Send prompts, output result, exit.
 *
 * Used for:
 * - `pi -p "prompt"` - text output
 * - `pi --mode json "prompt"` - JSON event stream
 */
import { flushRawStdout, writeRawStdout } from "../core/output-guard.js";
/**
 * Run in print (single-shot) mode.
 * Sends prompts to the agent and outputs the result.
 */
export async function runPrintMode(session, options) {
    const { mode, messages = [], initialMessage, initialImages } = options;
    let exitCode = 0;
    try {
        if (mode === "json") {
            const header = session.sessionManager.getHeader();
            if (header) {
                writeRawStdout(`${JSON.stringify(header)}\n`);
            }
        }
        // Set up extensions for print mode (no UI)
        await session.bindExtensions({
            commandContextActions: {
                waitForIdle: () => session.agent.waitForIdle(),
                newSession: async (options) => {
                    const success = await session.newSession({ parentSession: options?.parentSession });
                    if (success && options?.setup) {
                        await options.setup(session.sessionManager);
                    }
                    return { cancelled: !success };
                },
                fork: async (entryId) => {
                    const result = await session.fork(entryId);
                    return { cancelled: result.cancelled };
                },
                navigateTree: async (targetId, options) => {
                    const result = await session.navigateTree(targetId, {
                        summarize: options?.summarize,
                        customInstructions: options?.customInstructions,
                        replaceInstructions: options?.replaceInstructions,
                        label: options?.label,
                    });
                    return { cancelled: result.cancelled };
                },
                switchSession: async (sessionPath) => {
                    const success = await session.switchSession(sessionPath);
                    return { cancelled: !success };
                },
                reload: async () => {
                    await session.reload();
                },
            },
            onError: (err) => {
                console.error(`Extension error (${err.extensionPath}): ${err.error}`);
            },
        });
        // Always subscribe to enable session persistence via _handleAgentEvent
        session.subscribe((event) => {
            // In JSON mode, output all events
            if (mode === "json") {
                writeRawStdout(`${JSON.stringify(event)}\n`);
            }
        });
        // Send initial message with attachments
        if (initialMessage) {
            await session.prompt(initialMessage, { images: initialImages });
        }
        // Send remaining messages
        for (const message of messages) {
            await session.prompt(message);
        }
        // In text mode, output final response
        if (mode === "text") {
            const state = session.state;
            const lastMessage = state.messages[state.messages.length - 1];
            if (lastMessage?.role === "assistant") {
                const assistantMsg = lastMessage;
                // Check for error/aborted
                if (assistantMsg.stopReason === "error" || assistantMsg.stopReason === "aborted") {
                    console.error(assistantMsg.errorMessage || `Request ${assistantMsg.stopReason}`);
                    exitCode = 1;
                }
                else {
                    // Output text content
                    for (const content of assistantMsg.content) {
                        if (content.type === "text") {
                            writeRawStdout(`${content.text}\n`);
                        }
                    }
                }
            }
        }
        return exitCode;
    }
    finally {
        const extensionRunner = session.extensionRunner;
        if (extensionRunner?.hasHandlers("session_shutdown")) {
            await extensionRunner.emit({ type: "session_shutdown" });
        }
        // Ensure stdout is fully flushed before returning
        // This prevents race conditions where the process exits before all output is written
        await flushRawStdout();
    }
}
//# sourceMappingURL=print-mode.js.map