//#region src/browser/snapshot-helpers.ts
/**
* Snapshot intelligence helpers — structure-aware truncation for ARIA snapshots.
*
* ARIA snapshots use indentation-based YAML-like trees. Naive character-based
* truncation can split in the middle of a node, making the output confusing.
* These helpers preserve tree-node boundaries and provide metadata.
*/
/**
* Truncate an ARIA snapshot at a line boundary, preserving complete tree nodes.
*
* Strategy:
* 1. If the text is within `maxLength`, return as-is.
* 2. Find the last newline before `maxLength`.
* 3. Walk backwards to find a line at indentation level ≤ 2 (top-level nodes),
*    ensuring we don't cut inside a deeply nested subtree.
* 4. Append a truncation notice with stats.
*/
function truncateSnapshotAtBoundary(text, maxLength) {
	if (text.length <= maxLength) return text;
	let cutoff = text.lastIndexOf("\n", maxLength);
	if (cutoff <= 0) return `${text.slice(0, maxLength)}\n... (truncated)`;
	const searchStart = Math.max(0, cutoff - 2e3);
	const lines = text.slice(searchStart, cutoff).split("\n");
	let bestCutIndex = cutoff;
	let accumulated = cutoff;
	for (let i = lines.length - 1; i >= 0; i--) {
		const line = lines[i];
		const indent = line.search(/\S/);
		if (indent >= 0 && indent <= 2) {
			bestCutIndex = accumulated;
			break;
		}
		accumulated -= line.length + 1;
	}
	const truncated = text.slice(0, bestCutIndex);
	const totalLines = text.split("\n").length;
	const shownLines = truncated.split("\n").length;
	return `${truncated}\n... (truncated: showing ${shownLines} of ${totalLines} lines, ${totalLines - shownLines} lines omitted)`;
}
/**
* Produce a compact summary header for a snapshot.
* Counts interactive elements (links, buttons, inputs) to help the agent
* understand page complexity at a glance.
*/
function snapshotSummaryHeader(snapshotText, pageUrl) {
	const lines = snapshotText.split("\n");
	const totalLines = lines.length;
	let linkCount = 0;
	let buttonCount = 0;
	let inputCount = 0;
	let imageCount = 0;
	for (const line of lines) {
		const trimmed = line.trimStart();
		if (trimmed.startsWith("- link ") || trimmed.startsWith("- link:")) linkCount++;
		if (trimmed.startsWith("- button ") || trimmed.startsWith("- button:")) buttonCount++;
		if (trimmed.startsWith("- textbox ") || trimmed.startsWith("- textbox:") || trimmed.startsWith("- searchbox ") || trimmed.startsWith("- combobox ")) inputCount++;
		if (trimmed.startsWith("- img ") || trimmed.startsWith("- img:")) imageCount++;
	}
	const parts = [`Page: ${pageUrl}`, `Snapshot: ${totalLines} lines`];
	const counts = [];
	if (linkCount > 0) counts.push(`${linkCount} links`);
	if (buttonCount > 0) counts.push(`${buttonCount} buttons`);
	if (inputCount > 0) counts.push(`${inputCount} inputs`);
	if (imageCount > 0) counts.push(`${imageCount} images`);
	if (counts.length > 0) parts.push(`Interactive: ${counts.join(", ")}`);
	return parts.join(" | ");
}
//#endregion
export { snapshotSummaryHeader, truncateSnapshotAtBoundary };

//# sourceMappingURL=snapshot-helpers.js.map