import { z } from 'zod';
import { config } from '../../config.js';
import { errorResult, jsonResult, toErrorMessage } from '../shared.js';
import { browserManager } from './browserManager.js';
export function registerBrowserScreenshotTool(server) {
    server.registerTool('browser_screenshot', {
        title: 'Browser Screenshot',
        description: 'Capture a screenshot of the current page or a specific element. Returns the image as a base64 data URI (PNG).',
        inputSchema: {
            selector: z.string().optional().describe('CSS selector of the element to screenshot. If omitted, captures the full page.'),
            fullPage: z
                .boolean()
                .optional()
                .default(false)
                .describe('Capture the full scrollable page (only used when selector is omitted).'),
        },
    }, async ({ selector, fullPage }) => {
        try {
            const session = await browserManager.getSession();
            let buffer;
            if (selector) {
                const element = await session.page.locator(selector).first();
                await element.waitFor({ state: 'visible', timeout: 5000 });
                buffer = await element.screenshot({ type: 'png' });
            }
            else {
                buffer = await session.page.screenshot({ fullPage, type: 'png' });
            }
            const base64 = buffer.toString('base64');
            const dataUri = `data:image/png;base64,${base64}`;
            return jsonResult({
                selector: selector ?? null,
                fullPage: selector ? false : fullPage,
                sizeBytes: buffer.length,
                dataUri,
                truncatedDataUri: dataUri.length > config.maxToolOutputChars ? `${dataUri.slice(0, config.maxToolOutputChars)}...` : dataUri,
                note: 'The full dataUri is returned above. If it is truncated, use the returned base64 value directly from your client.',
            });
        }
        catch (error) {
            return errorResult(toErrorMessage(error));
        }
    });
}
//# sourceMappingURL=screenshot.js.map