import { Type } from "@sinclair/typebox";
import {
	getScreenshotFormatOverride,
	getScreenshotQualityDefault,
} from "../capture.js";
export function registerScreenshotTools(pi, deps) {
	pi.registerTool({
		name: "browser_screenshot",
		label: "Browser Screenshot",
		description:
			"Take a screenshot of the current browser page and return it as an inline image. Uses JPEG for viewport/fullpage (smaller, configurable quality) and PNG for element crops (preserves transparency). Optionally crop to a specific element by CSS selector.",
		parameters: Type.Object({
			fullPage: Type.Optional(
				Type.Boolean({
					description: "Capture the full scrollable page (default: false)",
				}),
			),
			selector: Type.Optional(
				Type.String({
					description:
						"CSS selector of a specific element to screenshot (crops to that element's bounding box). If omitted, screenshots the entire viewport.",
				}),
			),
			quality: Type.Optional(
				Type.Number({
					description:
						"JPEG quality 1-100 (default: 80). Only applies to viewport/fullpage screenshots, not element crops. Lower = smaller image.",
				}),
			),
		}),
		async execute(_toolCallId, params, _signal, _onUpdate, _ctx) {
			try {
				const { page: p } = await deps.ensureBrowser();
				let screenshotBuffer;
				let mimeType;
				const formatOverride = getScreenshotFormatOverride();
				const quality = params.quality ?? getScreenshotQualityDefault(80);
				if (params.selector) {
					const fmt = formatOverride ?? "png";
					const locator = p.locator(params.selector).first();
					if (fmt === "jpeg") {
						screenshotBuffer = await locator.screenshot({
							type: "jpeg",
							quality,
							scale: "css",
						});
						mimeType = "image/jpeg";
					} else {
						screenshotBuffer = await locator.screenshot({
							type: "png",
							scale: "css",
						});
						mimeType = "image/png";
					}
				} else {
					const fmt = formatOverride ?? "jpeg";
					if (fmt === "png") {
						screenshotBuffer = await p.screenshot({
							fullPage: params.fullPage ?? false,
							type: "png",
							scale: "css",
						});
						mimeType = "image/png";
					} else {
						screenshotBuffer = await p.screenshot({
							fullPage: params.fullPage ?? false,
							type: "jpeg",
							quality,
							scale: "css",
						});
						mimeType = "image/jpeg";
					}
				}
				screenshotBuffer = await deps.constrainScreenshot(
					p,
					screenshotBuffer,
					mimeType,
					quality,
				);
				const base64Data = screenshotBuffer.toString("base64");
				const title = await p.title();
				const url = p.url();
				const viewport = p.viewportSize();
				const vpText = viewport
					? `${viewport.width}x${viewport.height}`
					: "unknown";
				const scope = params.selector
					? `element "${params.selector}"`
					: params.fullPage
						? "full page"
						: "viewport";
				return {
					content: [
						{
							type: "text",
							text: `Screenshot of ${scope}.\nPage: ${title}\nURL: ${url}\nViewport: ${vpText}`,
						},
						{
							type: "image",
							data: base64Data,
							mimeType,
						},
					],
					details: { title, url, scope, viewport: vpText },
				};
			} catch (err) {
				return {
					content: [
						{ type: "text", text: `Screenshot failed: ${err.message}` },
					],
					details: { error: err.message },
					isError: true,
				};
			}
		},
	});
}
