/**
 * Core browser tools: navigate, navigate_back, close, resize.
 */
export function registerCoreTools(ext, bs) {
    const navigateTool = {
        name: "browser_navigate",
        label: "Browser Navigate",
        description: "Navigate to a URL in the browser. Auto-prepends https:// if no protocol is given.",
        promptSnippet: "`browser_navigate { url }` — navigate the browser to a URL",
        parameters: {
            type: "object",
            properties: {
                url: {
                    type: "string",
                    description: "The URL to navigate to. https:// is prepended automatically if missing.",
                },
            },
            required: ["url"],
        },
        async execute(_toolCallId, params) {
            const { url } = params;
            if (!url || typeof url !== "string") {
                return { content: [{ type: "text", text: "Error: url (string) is required." }], details: {} };
            }
            try {
                const result = await bs.navigate(url);
                return {
                    content: [{ type: "text", text: `✓ Navigated to: ${result.url}\nTitle: ${result.title}` }],
                    details: {},
                };
            }
            catch (err) {
                const msg = err instanceof Error ? err.message : String(err);
                return { content: [{ type: "text", text: `Navigation failed: ${msg}` }], details: { isError: true } };
            }
        },
    };
    const navigateBackTool = {
        name: "browser_navigate_back",
        label: "Browser Navigate Back",
        description: "Go back to the previous page in browser history.",
        promptSnippet: "`browser_navigate_back` — go back one page",
        parameters: {
            type: "object",
            properties: {},
        },
        async execute() {
            try {
                const result = await bs.navigateBack();
                return {
                    content: [{ type: "text", text: `✓ Went back to: ${result.url}\nTitle: ${result.title}` }],
                    details: {},
                };
            }
            catch (err) {
                const msg = err instanceof Error ? err.message : String(err);
                return { content: [{ type: "text", text: `Navigate back failed: ${msg}` }], details: { isError: true } };
            }
        },
    };
    const closeTool = {
        name: "browser_close",
        label: "Browser Close Page",
        description: "Close the current page. A blank page is opened if it was the last one.",
        promptSnippet: "`browser_close` — close the current browser page",
        parameters: {
            type: "object",
            properties: {},
        },
        async execute() {
            try {
                await bs.closePage();
                return { content: [{ type: "text", text: "✓ Page closed." }], details: {} };
            }
            catch (err) {
                const msg = err instanceof Error ? err.message : String(err);
                return { content: [{ type: "text", text: `Close failed: ${msg}` }], details: { isError: true } };
            }
        },
    };
    const resizeTool = {
        name: "browser_resize",
        label: "Browser Resize",
        description: "Resize the browser viewport.",
        promptSnippet: "`browser_resize { width, height }` — resize the browser viewport",
        parameters: {
            type: "object",
            properties: {
                width: { type: "number", description: "Width in pixels" },
                height: { type: "number", description: "Height in pixels" },
            },
            required: ["width", "height"],
        },
        async execute(_toolCallId, params) {
            const { width, height } = params;
            try {
                await bs.resize(width, height);
                return { content: [{ type: "text", text: `✓ Viewport resized to ${width}x${height}.` }], details: {} };
            }
            catch (err) {
                const msg = err instanceof Error ? err.message : String(err);
                return { content: [{ type: "text", text: `Resize failed: ${msg}` }], details: { isError: true } };
            }
        },
    };
    for (const tool of [navigateTool, navigateBackTool, closeTool, resizeTool]) {
        try {
            ext.registerTool(tool);
        }
        catch (err) {
            const msg = err instanceof Error ? err.message : String(err);
            process.stderr.write(`[browser] Failed to register ${tool.name}: ${msg}\n`);
        }
    }
}
