"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerPageInteractionTools = registerPageInteractionTools;
const browser_lifecycle_1 = require("./browser-lifecycle");
const schemas_1 = require("./schemas");
function registerPageInteractionTools(srv) {
    // ── click ────────────────────────────────────────────────
    srv.registerTool('click', {
        title: 'Click Element',
        description: 'Click an element on the page by CSS selector. Accepts _handle for cross-activity access.',
        inputSchema: schemas_1.clickSchema,
    }, async (args) => {
        try {
            const { page, pageId } = await (0, browser_lifecycle_1.resolvePage)(args);
            await page.click(args.selector, { timeout: 10_000 });
            await page.waitForTimeout(500);
            return {
                content: [{
                        type: 'text',
                        text: JSON.stringify({
                            clicked: args.selector,
                            url: page.url(),
                            title: await page.title(),
                            _handle: (0, browser_lifecycle_1.buildHandle)(pageId),
                        }),
                    }],
            };
        }
        catch (err) {
            if (err.code) {
                return { content: [{ type: 'text', text: JSON.stringify({ error: err.message, code: err.code }) }], isError: true };
            }
            throw err;
        }
    });
    // ── fill ─────────────────────────────────────────────────
    srv.registerTool('fill', {
        title: 'Fill Input',
        description: 'Type a value into an input field. Accepts _handle for cross-activity access.',
        inputSchema: schemas_1.fillSchema,
    }, async (args) => {
        try {
            const { page, pageId } = await (0, browser_lifecycle_1.resolvePage)(args);
            await page.fill(args.selector, args.value, { timeout: 10_000 });
            return {
                content: [{
                        type: 'text',
                        text: JSON.stringify({
                            filled: args.selector,
                            value: args.value,
                            _handle: (0, browser_lifecycle_1.buildHandle)(pageId),
                        }),
                    }],
            };
        }
        catch (err) {
            if (err.code) {
                return { content: [{ type: 'text', text: JSON.stringify({ error: err.message, code: err.code }) }], isError: true };
            }
            throw err;
        }
    });
    // ── wait_for ─────────────────────────────────────────────
    srv.registerTool('wait_for', {
        title: 'Wait for Element',
        description: 'Wait for an element to appear. Accepts _handle for cross-activity access.',
        inputSchema: schemas_1.waitForSchema,
    }, async (args) => {
        try {
            const { page, pageId } = await (0, browser_lifecycle_1.resolvePage)(args);
            await page.waitForSelector(args.selector, {
                timeout: args.timeout ?? 10_000,
            });
            return {
                content: [{
                        type: 'text',
                        text: JSON.stringify({
                            found: args.selector,
                            _handle: (0, browser_lifecycle_1.buildHandle)(pageId),
                        }),
                    }],
            };
        }
        catch (err) {
            if (err.code) {
                return { content: [{ type: 'text', text: JSON.stringify({ error: err.message, code: err.code }) }], isError: true };
            }
            throw err;
        }
    });
    // ── evaluate ─────────────────────────────────────────────
    srv.registerTool('evaluate', {
        title: 'Run JavaScript',
        description: 'Evaluate JavaScript in the page context. Accepts _handle for cross-activity access.',
        inputSchema: schemas_1.evaluateSchema,
    }, async (args) => {
        try {
            const { page, pageId } = await (0, browser_lifecycle_1.resolvePage)(args);
            const result = await page.evaluate(args.script);
            return {
                content: [{
                        type: 'text',
                        text: JSON.stringify({
                            result,
                            _handle: (0, browser_lifecycle_1.buildHandle)(pageId),
                        }),
                    }],
            };
        }
        catch (err) {
            if (err.code) {
                return { content: [{ type: 'text', text: JSON.stringify({ error: err.message, code: err.code }) }], isError: true };
            }
            throw err;
        }
    });
    // ── list_pages ───────────────────────────────────────────
    srv.registerTool('list_pages', {
        title: 'List Open Pages',
        description: 'List all open browser pages with their IDs, URLs, and titles.',
        inputSchema: schemas_1.listPagesSchema,
    }, async (_args) => {
        const result = [];
        for (const [id, page] of browser_lifecycle_1.pages) {
            result.push({
                page_id: id,
                url: page.url(),
                title: await page.title(),
            });
        }
        return {
            content: [{
                    type: 'text',
                    text: JSON.stringify({ pages: result, count: result.length }),
                }],
        };
    });
    // ── close_page ───────────────────────────────────────────
    srv.registerTool('close_page', {
        title: 'Close Page',
        description: 'Close a browser page by its ID. Accepts _handle for cross-activity access.',
        inputSchema: schemas_1.closePageSchema,
    }, async (args) => {
        try {
            const { page, pageId } = await (0, browser_lifecycle_1.resolvePage)(args);
            await page.close();
            browser_lifecycle_1.pages.delete(pageId);
            return {
                content: [{
                        type: 'text',
                        text: JSON.stringify({ closed: pageId }),
                    }],
            };
        }
        catch (err) {
            if (err.code) {
                return { content: [{ type: 'text', text: JSON.stringify({ error: err.message, code: err.code }) }], isError: true };
            }
            throw err;
        }
    });
}
