/**
 * Normalizes an email: trims whitespace and lowercases.
 */
export function normalizeEmail(value: string): string {
	return value.trim().toLowerCase();
}

/**
 * Validates an email address.
 * Checks: non-empty, max 254 chars, passes a basic RFC-aware regex
 * and a mailto: URI test via HTMLInputElement.
 */
export function isValidEmail(email: string): boolean {
	const trimmed = email.trim();
	if (!trimmed) return false;
	if (trimmed.length > 254) return false;

	// Basic structural regex — no RFC 5322 full validation, but catches obvious junk
	if (!/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(trimmed)) return false;

	// Use the browser's built-in email validation via type=email
	// (catches things like multiple @ signs)
	const input = document.createElement('input');
	input.type = 'email';
	input.value = trimmed;
	return input.checkValidity();
}
