import { z } from 'zod';
import { errorResult, jsonResult, toErrorMessage } from '../shared.js';
import { browserManager } from './browserManager.js';
export function registerBrowserInteractTools(server) {
    server.registerTool('browser_click', {
        title: 'Browser Click',
        description: 'Click the first element matching a CSS selector on the current page.',
        inputSchema: {
            selector: z.string().min(1).describe('CSS selector of the element to click.'),
        },
    }, async ({ selector }) => {
        try {
            const session = await browserManager.getSession();
            await session.page.locator(selector).first().click();
            return jsonResult({ clicked: true, selector });
        }
        catch (error) {
            return errorResult(toErrorMessage(error));
        }
    });
    server.registerTool('browser_fill', {
        title: 'Browser Fill',
        description: 'Fill an input/textarea element with the provided text.',
        inputSchema: {
            selector: z.string().min(1).describe('CSS selector of the input element.'),
            value: z.string().describe('Text to type into the element.'),
            clearFirst: z
                .boolean()
                .optional()
                .default(true)
                .describe('Clear the existing value before filling.'),
        },
    }, async ({ selector, value, clearFirst }) => {
        try {
            const session = await browserManager.getSession();
            const locator = session.page.locator(selector).first();
            if (clearFirst)
                await locator.fill(value);
            else
                await locator.pressSequentially(value);
            return jsonResult({ filled: true, selector, value });
        }
        catch (error) {
            return errorResult(toErrorMessage(error));
        }
    });
    server.registerTool('browser_eval', {
        title: 'Browser Eval',
        description: 'Evaluate a JavaScript function in the context of the current page and return the result. The function is stringified and executed in the browser, so it can access the DOM (document, window, etc.).',
        inputSchema: {
            script: z
                .string()
                .min(1)
                .describe('JavaScript function body to evaluate. Must be a function expression returning a JSON-serializable value.'),
        },
    }, async ({ script }) => {
        try {
            const session = await browserManager.getSession();
            const wrapped = `(async () => { ${script} })()`;
            const result = await session.page.evaluate(wrapped);
            return jsonResult({ result });
        }
        catch (error) {
            return errorResult(toErrorMessage(error));
        }
    });
}
//# sourceMappingURL=interact.js.map