/**
 * Drop tool: browser_drop.
 */
export function registerDropTools(ext, bs) {
    const dropTool = {
        name: "browser_drop",
        label: "Browser Drop",
        description: "Drop files or MIME-typed data onto an element, as if dragged from outside the page. " +
            "At least one of 'paths' or 'data' must be provided.",
        promptSnippet: "`browser_drop { element, paths, data }` — drop files or data onto an element",
        parameters: {
            type: "object",
            properties: {
                element: {
                    type: "string",
                    description: "CSS selector of the drop target element.",
                },
                paths: {
                    type: "array",
                    items: { type: "string" },
                    description: "File paths to drop onto the element.",
                },
                data: {
                    type: "array",
                    items: {
                        type: "object",
                        properties: {
                            mimeType: { type: "string", description: "MIME type of the data (e.g. 'text/plain')." },
                            content: { type: "string", description: "The data content." },
                        },
                        required: ["mimeType", "content"],
                    },
                    description: "MIME-typed data items to drop.",
                },
            },
            required: ["element"],
        },
        async execute(_toolCallId, params) {
            const p = params;
            try {
                const page = bs.getActivePage();
                if (!p.paths && !p.data) {
                    return {
                        content: [{ type: "text", text: "Error: At least one of 'paths' or 'data' must be provided." }],
                        details: { isError: true },
                    };
                }
                // Dispatch the drag-and-drop via dataTransfer
                await page.evaluate(({ element, paths, data }) => {
                    const target = document.querySelector(element);
                    if (!target)
                        throw new Error(`Element not found: ${element}`);
                    const dt = new DataTransfer();
                    if (paths) {
                        // Note: DataTransfer files from paths won't actually contain file data
                        // in most browser contexts — use data for inline content instead
                        for (const path of paths) {
                            dt.items.add(path, "application/octet-stream");
                        }
                    }
                    if (data) {
                        for (const item of data) {
                            dt.setData(item.mimeType, item.content);
                        }
                    }
                    target.dispatchEvent(new DragEvent("drop", { dataTransfer: dt, bubbles: true }));
                }, { element: p.element, paths: p.paths, data: p.data });
                const droppedItems = [
                    ...(p.paths ?? []).map((path) => `file: ${path}`),
                    ...(p.data ?? []).map((d) => `${d.mimeType}: ${d.content.slice(0, 50)}`),
                ];
                return {
                    content: [{
                            type: "text",
                            text: `✓ Dropped onto ${p.element}:\n${droppedItems.map((i) => `  - ${i}`).join("\n")}`,
                        }],
                    details: {},
                };
            }
            catch (err) {
                const msg = err instanceof Error ? err.message : String(err);
                return { content: [{ type: "text", text: `Drop failed: ${msg}` }], details: { isError: true } };
            }
        },
    };
    try {
        ext.registerTool(dropTool);
    }
    catch (err) {
        const msg = err instanceof Error ? err.message : String(err);
        process.stderr.write(`[browser] Failed to register browser_drop: ${msg}\n`);
    }
}
