/**
 * Network tools: browser_network_requests, browser_network_request.
 */
export function registerNetworkTools(ext, bs) {
    const networkRequestsTool = {
        name: "browser_network_requests",
        label: "Browser Network Requests",
        description: "Returns a numbered list of network requests since loading the page. " +
            "Use browser_network_request with the index number to get full details of a specific request.",
        promptSnippet: "`browser_network_requests` — list all network requests",
        parameters: {
            type: "object",
            properties: {
                includeStatic: {
                    type: "boolean",
                    description: "Include static assets (images, fonts, CSS). Default: false (shows only XHR/fetch).",
                },
            },
        },
        async execute(_toolCallId, params) {
            const p = params;
            try {
                const allRequests = bs.getNetworkRequests();
                const filtered = p.includeStatic
                    ? allRequests
                    : allRequests.filter((r) => {
                        const url = r.url.toLowerCase();
                        // Filter out common static assets
                        if (url.match(/\.(png|jpg|jpeg|gif|svg|ico|webp|woff|woff2|ttf|eot|css|js)(\?|$)/))
                            return false;
                        return true;
                    });
                if (filtered.length === 0) {
                    return { content: [{ type: "text", text: "No network requests recorded yet. Navigate to a page first." }], details: {} };
                }
                const lines = filtered.map((r) => `[${r.index}] ${r.method} ${r.status} ${r.url.slice(0, 120)}`);
                return { content: [{ type: "text", text: lines.join("\n") }], details: {} };
            }
            catch (err) {
                const msg = err instanceof Error ? err.message : String(err);
                return { content: [{ type: "text", text: `Network requests failed: ${msg}` }], details: { isError: true } };
            }
        },
    };
    const networkRequestTool = {
        name: "browser_network_request",
        label: "Browser Network Request Detail",
        description: "Returns full details (headers and body) of a single network request. " +
            "Use the index number from browser_network_requests.",
        promptSnippet: "`browser_network_request { index }` — get full request details",
        parameters: {
            type: "object",
            properties: {
                index: {
                    type: "number",
                    description: "The request index from browser_network_requests output.",
                },
                part: {
                    type: "string",
                    enum: ["request", "response", "both"],
                    description: "Which part to return. Default: both.",
                },
            },
            required: ["index"],
        },
        async execute(_toolCallId, params) {
            const p = params;
            try {
                const req = bs.getNetworkRequest(p.index);
                if (!req) {
                    return { content: [{ type: "text", text: `No request found at index ${p.index}.` }], details: {} };
                }
                const lines = [`[${req.index}] ${req.method} ${req.url}`, `Status: ${req.status} ${req.statusText}`];
                if (!p.part || p.part === "request" || p.part === "both") {
                    lines.push("\n--- Request Headers ---");
                    for (const [k, v] of Object.entries(req.headers)) {
                        lines.push(`${k}: ${v}`);
                    }
                    if (req.postData) {
                        lines.push(`\n--- Request Body ---\n${req.postData.slice(0, 10_000)}`);
                    }
                }
                if (!p.part || p.part === "response" || p.part === "both") {
                    lines.push("\n--- Response Headers ---");
                    for (const [k, v] of Object.entries(req.responseHeaders)) {
                        lines.push(`${k}: ${v}`);
                    }
                    if (req.body) {
                        lines.push(`\n--- Response Body ---\n${req.body.slice(0, 20_000)}`);
                    }
                }
                const text = lines.join("\n");
                return {
                    content: [{
                            type: "text",
                            text: text.length > 50_000 ? text.slice(0, 50_000) + "\n\n[truncated]" : text,
                        }],
                    details: {},
                };
            }
            catch (err) {
                const msg = err instanceof Error ? err.message : String(err);
                return { content: [{ type: "text", text: `Network request detail failed: ${msg}` }], details: { isError: true } };
            }
        },
    };
    for (const tool of [networkRequestsTool, networkRequestTool]) {
        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`);
        }
    }
}
