/** Normalize CI `$studentName` string | string[] into display lists */
export function normalizeStudentNames(studentName: string | string[] | undefined): string[] {
  if (studentName == null) return []
  if (Array.isArray(studentName))
    return studentName.map((n) => String(n).trim()).filter(Boolean)
  const s = String(studentName).trim()
  if (!s) return []
  return s
    .split(/\s*,\s*|\s*,?\s+and\s+/i)
    .map((x) => x.trim())
    .filter(Boolean)
}

export function joinNamesAnd(names: string[]): string {
  const n = names.length
  if (n === 0) return ''
  if (n === 1) return names[0]!
  if (n === 2) return `${names[0]} and ${names[1]}`
  return `${names.slice(0, -1).join(', ')}, and ${names[n - 1]}`
}

export const PORTAL_HREF = 'https://alrahmaisgl.org'

export function formatLocalDate(d: string | undefined): string {
  if (!d) return '—'
  const t = Date.parse(d)
  if (Number.isNaN(t)) return d
  return new Date(t).toLocaleDateString(undefined, {
    month: '2-digit',
    day: '2-digit',
    year: 'numeric',
  })
}
