// Utility functions for tool displays

/**
 * Format content by truncating long text intelligently
 */
export function formatContent(content: string, maxLength: number = 500): string {
	if (content.length <= maxLength) {
		return content;
	}

	const lines = content.split('\n');
	if (lines.length > 10) {
		return lines.slice(0, 5).join('\n') + 
			'\n... (' + (lines.length - 10) + ' more lines) ...\n' + 
			lines.slice(-5).join('\n');
	}
	return content.substring(0, maxLength) + '...';
}

/**
 * Truncate text with ellipsis
 */
export function truncateText(text: string, maxLength: number): string {
	if (text.length > maxLength) {
		return text.substring(0, maxLength - 3) + '...';
	}
	return text;
}

/**
 * Remove common indentation from a string
 */
export function removeCommonIndentation(input: string): string {
	const lines = input.split('\n');
	
	// Find minimum indentation across all non-empty lines
	let minIndent = Infinity;
	
	for (const line of lines) {
		if (line.trim()) { // Skip empty lines
			const match = line.match(/^(\s*)/);
			if (match) {
				minIndent = Math.min(minIndent, match[1].length);
			}
		}
	}
	
	// Remove common indentation from all lines
	if (minIndent > 0 && minIndent < Infinity) {
		return lines.map(line => {
			if (line.trim()) {
				return line.substring(minIndent);
			}
			return line;
		}).join('\n');
	}
	
	return input;
}

/**
 * Remove common indentation from an array of lines
 */
export function removeCommonIndentationFromLines(lines: string[]): { lines: string[], commonIndent: string } {
	// Find minimum indentation across all non-empty lines
	let minIndent = Infinity;
	let commonIndent = '';
	
	for (const line of lines) {
		if (line.trim()) { // Skip empty lines
			const match = line.match(/^(\s*)/);
			if (match) {
				const indent = match[1].length;
				if (indent < minIndent) {
					minIndent = indent;
					commonIndent = match[1].substring(0, minIndent);
				}
			}
		}
	}
	
	// Remove common indentation from all lines
	if (minIndent > 0 && minIndent < Infinity) {
		return {
			lines: lines.map(line => line.startsWith(commonIndent) ? line.substring(minIndent) : line),
			commonIndent
		};
	}

	return { lines, commonIndent: '' };
}

/**
 * Dedent two line-arrays jointly: find the common-indent across BOTH sides
 * combined, then strip the same prefix length from each side.
 *
 * Why: per-side dedent silently absorbs whitespace-only diffs. If `old` is
 * `[" :root {"]` and `new` is `[":root {"]`, dedenting each independently
 * makes both `[":root {"]` — the leading-space change vanishes and the
 * diff renders as "no changes". Using a joint min-indent preserves any
 * leading-whitespace difference while still trimming the shared indent
 * for clean display.
 */
export function removeCommonIndentationFromTwoSides(
	oldLines: string[],
	newLines: string[],
): { oldLines: string[]; newLines: string[]; commonIndent: string } {
	const { commonIndent } = removeCommonIndentationFromLines([...oldLines, ...newLines]);
	if (!commonIndent) return { oldLines, newLines, commonIndent: '' };
	const strip = commonIndent.length;
	const apply = (line: string) => (line.startsWith(commonIndent) ? line.substring(strip) : line);
	return {
		oldLines: oldLines.map(apply),
		newLines: newLines.map(apply),
		commonIndent,
	};
}