import { t as callGatewayTool } from "./gateway-C7Z2q325.js";

//#region src/shared/node-list-parse.ts
function asRecord(value) {
	return typeof value === "object" && value !== null ? value : {};
}
function parsePairingList(value) {
	const obj = asRecord(value);
	return {
		pending: Array.isArray(obj.pending) ? obj.pending : [],
		paired: Array.isArray(obj.paired) ? obj.paired : []
	};
}
function parseNodeList(value) {
	const obj = asRecord(value);
	return Array.isArray(obj.nodes) ? obj.nodes : [];
}

//#endregion
//#region src/shared/node-match.ts
function normalizeNodeKey(value) {
	return value.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-+/, "").replace(/-+$/, "");
}
function listKnownNodes(nodes) {
	return nodes.map((n) => n.displayName || n.remoteIp || n.nodeId).filter(Boolean).join(", ");
}
function resolveNodeMatches(nodes, query) {
	const q = query.trim();
	if (!q) return [];
	const qNorm = normalizeNodeKey(q);
	return nodes.filter((n) => {
		if (n.nodeId === q) return true;
		if (typeof n.remoteIp === "string" && n.remoteIp === q) return true;
		const name = typeof n.displayName === "string" ? n.displayName : "";
		if (name && normalizeNodeKey(name) === qNorm) return true;
		if (q.length >= 6 && n.nodeId.startsWith(q)) return true;
		return false;
	});
}
function resolveNodeIdFromCandidates(nodes, query) {
	const q = query.trim();
	if (!q) throw new Error("node required");
	const matches = resolveNodeMatches(nodes, q);
	if (matches.length === 1) return matches[0]?.nodeId ?? "";
	if (matches.length === 0) {
		const known = listKnownNodes(nodes);
		throw new Error(`unknown node: ${q}${known ? ` (known: ${known})` : ""}`);
	}
	throw new Error(`ambiguous node: ${q} (matches: ${matches.map((n) => n.displayName || n.remoteIp || n.nodeId).join(", ")})`);
}

//#endregion
//#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 };