/**
 * Wait tool: browser_wait_for.
 */
export function registerWaitTools(ext, bs) {
    const waitTool = {
        name: "browser_wait_for",
        label: "Browser Wait For",
        description: "Wait for text to appear, text to disappear, or a specified amount of time to pass.",
        promptSnippet: "`browser_wait_for { text, textGone, time }` — wait for a condition",
        parameters: {
            type: "object",
            properties: {
                text: {
                    type: "string",
                    description: "Wait until this text appears on the page.",
                },
                textGone: {
                    type: "string",
                    description: "Wait until this text disappears from the page.",
                },
                time: {
                    type: "number",
                    description: "Wait for this many milliseconds.",
                },
            },
        },
        async execute(_toolCallId, params) {
            const p = params;
            try {
                const page = bs.getActivePage();
                if (p.text) {
                    await page.waitForSelector(`text=${p.text}`, { timeout: 30_000 });
                    return { content: [{ type: "text", text: `✓ Text appeared: "${p.text}"` }], details: {} };
                }
                if (p.textGone) {
                    await page.waitForSelector(`text=${p.textGone}`, {
                        state: "detached",
                        timeout: 30_000,
                    });
                    return { content: [{ type: "text", text: `✓ Text disappeared: "${p.textGone}"` }], details: {} };
                }
                if (p.time) {
                    await page.waitForTimeout(p.time);
                    return { content: [{ type: "text", text: `✓ Waited ${p.time}ms` }], details: {} };
                }
                return {
                    content: [{ type: "text", text: "No wait condition specified. Provide text, textGone, or time." }],
                    details: { isError: true },
                };
            }
            catch (err) {
                const msg = err instanceof Error ? err.message : String(err);
                return { content: [{ type: "text", text: `Wait failed: ${msg}` }], details: { isError: true } };
            }
        },
    };
    try {
        ext.registerTool(waitTool);
    }
    catch (err) {
        const msg = err instanceof Error ? err.message : String(err);
        process.stderr.write(`[browser] Failed to register browser_wait_for: ${msg}\n`);
    }
}
