import "./paths-D4_LjrY7.js";
import "./utils-BnlaT0eO.js";
import "./theme-CG-K7m8u.js";
import "./subsystem-ocBR131w.js";
import "./runtime-CRvfzclG.js";
import "./exec-CZr_EJHt.js";
import "./agent-scope-CVnlY0oG.js";
import "./model-selection-p6J3l4qF.js";
import "./github-copilot-token-CjSKohyA.js";
import "./emitter-oHo8y9ow.js";
import "./sqlite-wYNyDR1B.js";
import "./reliability-Dl7hbRzF.js";
import "./boolean-DGGu1sGA.js";
import "./env-CvUd308i.js";
import "./validation-p1IK70TM.js";
import "./config-DJ3A5x_1.js";
import "./manifest-registry-U2EDFVP4.js";
import { d as imageMimeFromFormat } from "./image-ops-C0D77XHD.js";
import "./message-channel-DSU96OKy.js";
import { i as resolveImageSanitizationLimits } from "./tool-images-CRPqucMe.js";
import { a as jsonResult, r as imageResult, u as readStringParam } from "./common-oaZdjdal.js";
import "./client-DNPnFy4E.js";
import "./call-C206Z-zx.js";
import { n as readGatewayCallOptions, t as callGatewayTool } from "./gateway-DKSm1BrZ.js";
import { n as resolveNodeId } from "./nodes-utils-yMGpHJYN.js";
import { i as stringEnum, r as optionalStringEnum } from "./typebox-BNGTpPE2.js";
import { i as writeBase64ToFile } from "./nodes-camera-Dx4hh0R6.js";
import { n as parseCanvasSnapshotPayload, t as canvasSnapshotTempPath } from "./nodes-canvas-D--jyOrZ.js";
import fs from "node:fs/promises";
import crypto from "node:crypto";
import { Type } from "@sinclair/typebox";

//#region src/agents/tools/ui/canvas-tool.ts
const CanvasToolSchema = Type.Object({
	action: stringEnum([
		"present",
		"hide",
		"navigate",
		"eval",
		"snapshot",
		"a2ui_push",
		"a2ui_reset"
	]),
	gatewayUrl: Type.Optional(Type.String()),
	gatewayToken: Type.Optional(Type.String()),
	timeoutMs: Type.Optional(Type.Number()),
	node: Type.Optional(Type.String()),
	target: Type.Optional(Type.String()),
	x: Type.Optional(Type.Number()),
	y: Type.Optional(Type.Number()),
	width: Type.Optional(Type.Number()),
	height: Type.Optional(Type.Number()),
	url: Type.Optional(Type.String()),
	javaScript: Type.Optional(Type.String()),
	outputFormat: optionalStringEnum([
		"png",
		"jpg",
		"jpeg"
	]),
	maxWidth: Type.Optional(Type.Number()),
	quality: Type.Optional(Type.Number()),
	delayMs: Type.Optional(Type.Number()),
	jsonl: Type.Optional(Type.String()),
	jsonlPath: Type.Optional(Type.String())
});
function createCanvasTool(options) {
	const imageSanitization = resolveImageSanitizationLimits(options?.config);
	return {
		label: "Canvas",
		name: "canvas",
		description: "Control node canvases (present/hide/navigate/eval/snapshot/A2UI). Use snapshot to capture the rendered UI.",
		parameters: CanvasToolSchema,
		execute: async (_toolCallId, args) => {
			const params = args;
			const action = readStringParam(params, "action", { required: true });
			const gatewayOpts = readGatewayCallOptions(params);
			const nodeId = await resolveNodeId(gatewayOpts, readStringParam(params, "node", { trim: true }), true);
			const invoke = async (command, invokeParams) => await callGatewayTool("node.invoke", gatewayOpts, {
				nodeId,
				command,
				params: invokeParams,
				idempotencyKey: crypto.randomUUID()
			});
			switch (action) {
				case "present": {
					const placement = {
						x: typeof params.x === "number" ? params.x : void 0,
						y: typeof params.y === "number" ? params.y : void 0,
						width: typeof params.width === "number" ? params.width : void 0,
						height: typeof params.height === "number" ? params.height : void 0
					};
					const invokeParams = {};
					const presentTarget = readStringParam(params, "target", { trim: true }) ?? readStringParam(params, "url", { trim: true });
					if (presentTarget) invokeParams.url = presentTarget;
					if (Number.isFinite(placement.x) || Number.isFinite(placement.y) || Number.isFinite(placement.width) || Number.isFinite(placement.height)) invokeParams.placement = placement;
					await invoke("canvas.present", invokeParams);
					return jsonResult({ ok: true });
				}
				case "hide":
					await invoke("canvas.hide", void 0);
					return jsonResult({ ok: true });
				case "navigate":
					await invoke("canvas.navigate", { url: readStringParam(params, "url", { trim: true }) ?? readStringParam(params, "target", {
						required: true,
						trim: true,
						label: "url"
					}) });
					return jsonResult({ ok: true });
				case "eval": {
					const result = (await invoke("canvas.eval", { javaScript: readStringParam(params, "javaScript", { required: true }) }))?.payload?.result;
					if (result) return {
						content: [{
							type: "text",
							text: result
						}],
						details: { result }
					};
					return jsonResult({ ok: true });
				}
				case "snapshot": {
					const formatRaw = typeof params.outputFormat === "string" ? params.outputFormat.toLowerCase() : "png";
					const payload = parseCanvasSnapshotPayload((await invoke("canvas.snapshot", {
						format: formatRaw === "jpg" || formatRaw === "jpeg" ? "jpeg" : "png",
						maxWidth: typeof params.maxWidth === "number" && Number.isFinite(params.maxWidth) ? params.maxWidth : void 0,
						quality: typeof params.quality === "number" && Number.isFinite(params.quality) ? params.quality : void 0
					}))?.payload);
					const filePath = canvasSnapshotTempPath({ ext: payload.format === "jpeg" ? "jpg" : payload.format });
					await writeBase64ToFile(filePath, payload.base64);
					const mimeType = imageMimeFromFormat(payload.format) ?? "image/png";
					return await imageResult({
						label: "canvas:snapshot",
						path: filePath,
						base64: payload.base64,
						mimeType,
						details: { format: payload.format },
						imageSanitization
					});
				}
				case "a2ui_push": {
					const jsonl = typeof params.jsonl === "string" && params.jsonl.trim() ? params.jsonl : typeof params.jsonlPath === "string" && params.jsonlPath.trim() ? await fs.readFile(params.jsonlPath.trim(), "utf8") : "";
					if (!jsonl.trim()) throw new Error("jsonl or jsonlPath required");
					await invoke("canvas.a2ui.pushJSONL", { jsonl });
					return jsonResult({ ok: true });
				}
				case "a2ui_reset":
					await invoke("canvas.a2ui.reset", void 0);
					return jsonResult({ ok: true });
				default: throw new Error(`Unknown action: ${action}`);
			}
		}
	};
}

//#endregion
export { createCanvasTool };