/**
 * Statement email formatters — customer-statement (wave-16, spec 183).
 *
 * Locale-aware subject and body generators for the statement email.
 * All interpolated values are escaped.
 */

function esc(s: string): string {
  return s
    .replace(/&/g, '&amp;')
    .replace(/</g, '&lt;')
    .replace(/>/g, '&gt;')
    .replace(/"/g, '&quot;')
    .replace(/'/g, '&#39;')
}

export function formatStatementEmailSubject(
  customerName: string,
  from: string,
  to: string,
  locale: 'he' | 'en',
): string {
  const name = esc(customerName)
  const f = esc(from)
  const t = esc(to)
  if (locale === 'he') {
    return `כרטסת חשבון — ${name} — ${f}–${t}`
  }
  return `Statement of Account — ${name} — ${f}–${t}`
}

export function formatStatementEmailBody(
  customerName: string,
  from: string,
  to: string,
  message: string | undefined,
  locale: 'he' | 'en',
): string {
  const name = esc(customerName)
  const f = esc(from)
  const t = esc(to)

  if (message) {
    return esc(message)
  }

  if (locale === 'he') {
    return `שלום ${name},\n\nמצורפת כרטסת החשבון שלך לתקופה ${f}–${t}.\n\nבברכה`
  }
  return `Dear ${name},\n\nPlease find attached your statement of account for the period ${f}–${t}.\n\nKind regards`
}
