/**
 * Extract hashtags from text content
 * Matches #word patterns followed by word boundary (space, punctuation, or end of text)
 * This prevents capturing incomplete hashtags during typing
 */
export function parseHashtags(text: string): string[] {
	// Match hashtags followed by word boundary, whitespace, punctuation, or end of string
	const hashtagRegex = /#([a-zA-Z][a-zA-Z0-9_]*)(?=[\s\p{P}]|$)/gu;
	const matches = text.matchAll(hashtagRegex);
	const tags = new Set<string>();

	for (const match of matches) {
		tags.add(match[1].toLowerCase());
	}

	return Array.from(tags);
}

/**
 * Highlight hashtags in text for display
 * Returns HTML with hashtags wrapped in styled spans
 * Only highlights complete hashtags (followed by space, punctuation, or end)
 */
export function highlightHashtags(text: string): string {
	const escaped = text
		.replace(/&/g, '&amp;')
		.replace(/</g, '&lt;')
		.replace(/>/g, '&gt;');

	return escaped.replace(
		/#([a-zA-Z][a-zA-Z0-9_]*)(?=[\s\p{P}]|$)/gu,
		'<span class="text-blue-600 font-medium">#$1</span>'
	);
}
