/**
 * Basic helpers around email formatting shared across components and composables.
 */

/**
 * Normalises an email by trimming whitespace and converting it to lowercase.
 */
export const normalizeEmail = (rawValue: string): string => {
  const value = (rawValue || '').trim()
  return value.toLowerCase()
}

/**
 * Masks an email address by hiding the characters between the first and last letters
 * of the local part. Useful for UI fragments where we want partial obfuscation.
 */
export const maskEmail = (rawValue: string): string => {
  const value = normalizeEmail(rawValue)
  if (!value) {
    return ''
  }

  const parts = value.split('@')
  const localPart = parts[0] ?? ''
  const domain = parts[1]
  if (!domain || !localPart) {
    return value
  }

  if (localPart.length <= 2) {
    return `${localPart[0] ?? ''}·@${domain}`
  }

  const start = localPart[0] ?? ''
  const end = localPart.slice(-1)
  const masked = '·'.repeat(Math.max(0, localPart.length - 2))

  return `${start}${masked}${end}@${domain}`
}
