"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.scrollVisionTool = exports.scrollTool = void 0;
const ai_1 = require("ai");
const zod_1 = require("zod");
const coordinateNormalization_js_1 = require("../utils/coordinateNormalization.js");
const screenshotHandler_js_1 = require("../utils/screenshotHandler.js");
/**
 * Simple scroll tool for DOM mode (non-grounding models).
 * No coordinates - scrolls from viewport center.
 */
const scrollTool = (v3) => (0, ai_1.tool)({
    description: "Scroll the page up or down by a percentage of the viewport height. Default is 80%, and what should be typically used for general page scrolling",
    inputSchema: zod_1.z.object({
        direction: zod_1.z.enum(["up", "down"]),
        percentage: zod_1.z.number().min(1).max(200).optional(),
    }),
    execute: async ({ direction, percentage = 80, }) => {
        v3.logger({
            category: "agent",
            message: `Agent calling tool: scroll`,
            level: 1,
            auxiliary: {
                arguments: {
                    value: JSON.stringify({ direction, percentage }),
                    type: "object",
                },
            },
        });
        const page = await v3.context.awaitActivePage();
        const { w, h } = await page.mainFrame().evaluate("({ w: window.innerWidth, h: window.innerHeight })");
        const scrollDistance = Math.round((h * percentage) / 100);
        const cx = Math.floor(w / 2);
        const cy = Math.floor(h / 2);
        const deltaY = direction === "up" ? -scrollDistance : scrollDistance;
        await page.scroll(cx, cy, 0, deltaY);
        v3.recordAgentReplayStep({
            type: "scroll",
            deltaX: 0,
            deltaY,
            anchor: { x: cx, y: cy },
        });
        return {
            success: true,
            message: `Scrolled ${percentage}% ${direction} (${scrollDistance}px)`,
            scrolledPixels: scrollDistance,
        };
    },
    toModelOutput: (result) => {
        return {
            type: "json",
            value: {
                success: result.success,
                message: result.message,
                scrolledPixels: result.scrolledPixels,
            },
        };
    },
});
exports.scrollTool = scrollTool;
/**
 * Scroll tool for hybrid mode (grounding models).
 * Supports optional coordinates for scrolling within nested scrollable elements.
 */
const scrollVisionTool = (v3, provider) => (0, ai_1.tool)({
    description: `Scroll the page up or down. For general page scrolling, no coordinates needed. Only provide coordinates when scrolling inside a nested scrollable element (e.g., a dropdown menu, modal with overflow, or scrollable sidebar). Default is 80%, and what should be typically used for general page scrolling`,
    inputSchema: zod_1.z.object({
        direction: zod_1.z.enum(["up", "down"]),
        coordinates: zod_1.z
            .array(zod_1.z.number())
            .optional()
            .describe("Only use coordinates for scrolling inside a nested scrollable element - provide (x, y) within that element"),
        percentage: zod_1.z.number().min(1).max(200).optional(),
    }),
    execute: async ({ direction, coordinates, percentage = 80, }) => {
        const page = await v3.context.awaitActivePage();
        const { w, h } = await page.mainFrame().evaluate("({ w: window.innerWidth, h: window.innerHeight })");
        // Process coordinates if provided, otherwise use viewport center
        let cx;
        let cy;
        if (coordinates) {
            const processed = (0, coordinateNormalization_js_1.processCoordinates)(coordinates[0], coordinates[1], provider, v3);
            cx = processed.x;
            cy = processed.y;
        }
        else {
            cx = Math.floor(w / 2);
            cy = Math.floor(h / 2);
        }
        v3.logger({
            category: "agent",
            message: `Agent calling tool: scroll`,
            level: 1,
            auxiliary: {
                arguments: {
                    value: JSON.stringify({
                        direction,
                        coordinates,
                        percentage,
                        processed: { cx, cy },
                    }),
                    type: "object",
                },
            },
        });
        const scrollDistance = Math.round((h * percentage) / 100);
        const deltaY = direction === "up" ? -scrollDistance : scrollDistance;
        await page.scroll(cx, cy, 0, deltaY);
        const screenshotBase64 = await (0, screenshotHandler_js_1.waitAndCaptureScreenshot)(page, 100);
        v3.recordAgentReplayStep({
            type: "scroll",
            deltaX: 0,
            deltaY,
            anchor: { x: cx, y: cy },
        });
        return {
            success: true,
            message: coordinates
                ? `Scrolled ${percentage}% ${direction} at (${cx}, ${cy})`
                : `Scrolled ${percentage}% ${direction}`,
            scrolledPixels: scrollDistance,
            screenshotBase64,
        };
    },
    toModelOutput: (result) => {
        const content = [
            {
                type: "text",
                text: JSON.stringify({
                    success: result.success,
                    message: result.message,
                    scrolledPixels: result.scrolledPixels,
                }),
            },
        ];
        if (result.screenshotBase64) {
            content.push({
                type: "media",
                mediaType: "image/png",
                data: result.screenshotBase64,
            });
        }
        return { type: "content", value: content };
    },
});
exports.scrollVisionTool = scrollVisionTool;
//# sourceMappingURL=scroll.js.map