/**
 * Console messages tool: browser_console_messages.
 */
export function registerConsoleTools(ext, bs) {
    const consoleTool = {
        name: "browser_console_messages",
        label: "Browser Console Messages",
        description: "Returns all console messages from the browser. Each level includes messages of more severe levels.",
        promptSnippet: "`browser_console_messages` — get browser console output",
        parameters: {
            type: "object",
            properties: {
                level: {
                    type: "string",
                    enum: ["error", "warning", "info", "debug"],
                    description: "Minimum severity level to return. Default: info (returns info, warning, and error).",
                },
                all: {
                    type: "boolean",
                    description: "Return all messages since the start of the session, not just the current page. Default: false.",
                },
            },
        },
        async execute(_toolCallId, params) {
            const p = params;
            try {
                const messages = bs.getConsoleMessages(p.level, p.all ?? false);
                if (messages.length === 0) {
                    return { content: [{ type: "text", text: "No console messages." }], details: {} };
                }
                const lines = messages.map((m) => `[${m.type.padEnd(7)}] ${m.text.slice(0, 300)}`);
                const text = lines.join("\n");
                return {
                    content: [{
                            type: "text",
                            text: text.length > 20_000 ? text.slice(0, 20_000) + "\n\n[truncated]" : text,
                        }],
                    details: {},
                };
            }
            catch (err) {
                const msg = err instanceof Error ? err.message : String(err);
                return { content: [{ type: "text", text: `Console messages failed: ${msg}` }], details: { isError: true } };
            }
        },
    };
    try {
        ext.registerTool(consoleTool);
    }
    catch (err) {
        const msg = err instanceof Error ? err.message : String(err);
        process.stderr.write(`[browser] Failed to register browser_console_messages: ${msg}\n`);
    }
}
