import { getThemeExports } from "./theme-manager.js";
//#region src/tui/tui-tool-diff.ts
const DIFF_TOOLS = new Set([
	"edit",
	"write",
	"apply_patch",
	"patch",
	"search_replace",
	"str_replace"
]);
function isDiffFriendlyTool(toolName) {
	const base = toolName.split("__").pop()?.toLowerCase() ?? toolName.toLowerCase();
	return DIFF_TOOLS.has(base);
}
function looksLikeUnifiedDiff(text) {
	const sample = text.trimStart();
	if (sample.startsWith("@@")) return true;
	const lines = sample.split("\n").slice(0, 20);
	let diffLines = 0;
	for (const line of lines) if (/^(\+\+\+|---|@@|[+-] |diff --git)/.test(line)) diffLines += 1;
	return diffLines >= 2;
}
/** Colorize unified diff lines for terminal display. */
function renderUnifiedDiff(text, maxLines = 80) {
	const palette = getThemeExports().palette;
	const fg = (hex) => (line) => `\x1b[38;2;${hexToRgb(hex)}m${line}\x1b[0m`;
	const added = fg(palette.success);
	const removed = fg(palette.error);
	const context = fg(palette.dim);
	const header = fg(palette.accentSoft);
	const lines = text.split("\n");
	return (lines.length > maxLines ? [...lines.slice(0, maxLines), "…"] : lines).map((line) => {
		if (line.startsWith("+++") || line.startsWith("---") || line.startsWith("@@")) return header(line);
		if (line.startsWith("+")) return added(line);
		if (line.startsWith("-")) return removed(line);
		if (line.startsWith("diff --git")) return header(line);
		return context(line);
	}).join("\n");
}
function hexToRgb(hex) {
	const h = hex.replace("#", "");
	return `${Number.parseInt(h.slice(0, 2), 16)};${Number.parseInt(h.slice(2, 4), 16)};${Number.parseInt(h.slice(4, 6), 16)}`;
}
//#endregion
export { isDiffFriendlyTool, looksLikeUnifiedDiff, renderUnifiedDiff };

//# sourceMappingURL=tui-tool-diff.js.map