import { t as callGatewayTool } from "./gateway-DKSm1BrZ.js";
import { n as parseNodeList, r as parsePairingList, t as resolveNodeIdFromCandidates } from "./node-match-CM7iJ6sE.js";

//#region src/agents/tools/misc/nodes-utils.ts
async function loadNodes(opts) {
	try {
		return parseNodeList(await callGatewayTool("node.list", opts, {}));
	} catch {
		const { paired } = parsePairingList(await callGatewayTool("node.pair.list", opts, {}));
		return paired.map((n) => ({
			nodeId: n.nodeId,
			displayName: n.displayName,
			platform: n.platform,
			remoteIp: n.remoteIp
		}));
	}
}
function pickDefaultNode(nodes) {
	const withCanvas = nodes.filter((n) => Array.isArray(n.caps) ? n.caps.includes("canvas") : true);
	if (withCanvas.length === 0) return null;
	const connected = withCanvas.filter((n) => n.connected);
	const candidates = connected.length > 0 ? connected : withCanvas;
	if (candidates.length === 1) return candidates[0];
	const local = candidates.filter((n) => n.platform?.toLowerCase().startsWith("mac") && typeof n.nodeId === "string" && n.nodeId.startsWith("mac-"));
	if (local.length === 1) return local[0];
	return null;
}
async function listNodes(opts) {
	return loadNodes(opts);
}
function resolveNodeIdFromList(nodes, query, allowDefault = false) {
	const q = String(query ?? "").trim();
	if (!q) {
		if (allowDefault) {
			const picked = pickDefaultNode(nodes);
			if (picked) return picked.nodeId;
		}
		throw new Error("node required");
	}
	return resolveNodeIdFromCandidates(nodes, q);
}
async function resolveNodeId(opts, query, allowDefault = false) {
	return resolveNodeIdFromList(await loadNodes(opts), query, allowDefault);
}

//#endregion
export { resolveNodeId as n, resolveNodeIdFromList as r, listNodes as t };