/**
 * Tab management tools: list, create, close, select tabs.
 */
export function registerTabsTools(ext, bs) {
    const tabsTool = {
        name: "browser_tabs",
        label: "Browser Tabs",
        description: "List, create, close, or select browser tabs.",
        promptSnippet: "`browser_tabs { action }` — manage browser tabs (list/create/close/select)",
        parameters: {
            type: "object",
            properties: {
                action: {
                    type: "string",
                    enum: ["list", "create", "close", "select"],
                    description: "Tab action: list all tabs, create a new one, close one, or select (switch to) one.",
                },
                index: {
                    type: "number",
                    description: "Tab index for close/select actions (0-based).",
                },
            },
            required: ["action"],
        },
        async execute(_toolCallId, params) {
            const p = params;
            try {
                switch (p.action) {
                    case "list": {
                        const tabs = bs.listTabs();
                        if (tabs.length === 0) {
                            return { content: [{ type: "text", text: "No tabs open." }], details: {} };
                        }
                        const lines = tabs.map((t) => `${t.index === bs["activePageIndex"] ? "▶" : " "} [${t.index}] ${t.url}`);
                        return { content: [{ type: "text", text: lines.join("\n") }], details: {} };
                    }
                    case "create": {
                        const { index } = await bs.createTab();
                        return { content: [{ type: "text", text: `✓ Created new tab [${index}]` }], details: {} };
                    }
                    case "close": {
                        if (p.index === undefined) {
                            return { content: [{ type: "text", text: "Error: 'index' is required for close action." }], details: { isError: true } };
                        }
                        await bs.closeTab(p.index);
                        return { content: [{ type: "text", text: `✓ Closed tab [${p.index}]` }], details: {} };
                    }
                    case "select": {
                        if (p.index === undefined) {
                            return { content: [{ type: "text", text: "Error: 'index' is required for select action." }], details: { isError: true } };
                        }
                        const result = await bs.selectTab(p.index);
                        return { content: [{ type: "text", text: `✓ Selected tab [${result.index}]` }], details: {} };
                    }
                    default:
                        return { content: [{ type: "text", text: `Unknown action: ${p.action}` }], details: { isError: true } };
                }
            }
            catch (err) {
                const msg = err instanceof Error ? err.message : String(err);
                return { content: [{ type: "text", text: `Tabs action failed: ${msg}` }], details: { isError: true } };
            }
        },
    };
    try {
        ext.registerTool(tabsTool);
    }
    catch (err) {
        const msg = err instanceof Error ? err.message : String(err);
        process.stderr.write(`[browser] Failed to register browser_tabs: ${msg}\n`);
    }
}
