/**
 * Dialog tool: browser_handle_dialog.
 *
 * NOTE: BrowserService intercepts all dialogs globally and holds them
 * until the agent handles them via this tool. This avoids Playwright's
 * default auto-dismiss behavior.
 */
export function registerDialogTools(ext, bs) {
    const dialogTool = {
        name: "browser_handle_dialog",
        label: "Browser Handle Dialog",
        description: "Handle a JavaScript dialog (alert, confirm, prompt) that appeared on the page. " +
            "Call this AFTER performing an action that triggers a dialog.",
        promptSnippet: "`browser_handle_dialog { accept, promptText }` — accept or dismiss a browser dialog",
        parameters: {
            type: "object",
            properties: {
                accept: {
                    type: "boolean",
                    description: "Whether to accept (true) or dismiss (false) the dialog.",
                },
                promptText: {
                    type: "string",
                    description: "Text to enter for prompt dialogs (only when accept is true).",
                },
            },
            required: ["accept"],
        },
        async execute(_toolCallId, params) {
            const p = params;
            try {
                const message = await bs.handleDialog(p.accept, p.promptText);
                const action = p.accept
                    ? (p.promptText ? `accepted with "${p.promptText}"` : "accepted")
                    : "dismissed";
                return {
                    content: [{ type: "text", text: `✓ Dialog ${action}: "${message}"` }],
                    details: {},
                };
            }
            catch (err) {
                const msg = err instanceof Error ? err.message : String(err);
                return { content: [{ type: "text", text: `Dialog handling failed: ${msg}` }], details: { isError: true } };
            }
        },
    };
    try {
        ext.registerTool(dialogTool);
    }
    catch (err) {
        const msg = err instanceof Error ? err.message : String(err);
        process.stderr.write(`[browser] Failed to register browser_handle_dialog: ${msg}\n`);
    }
}
