/** Mirrors `ClassProgressController::splitUnitTitleForDisplay` / CI `class_progress_unit_display.php`. */

export function formatProgressDate(value?: string | null, withTime = false) {
  if (!value) return '—'
  const date = new Date(value)
  if (Number.isNaN(date.getTime())) return value
  return withTime ? date.toLocaleString() : date.toLocaleDateString()
}

export function splitUnitTitle(unitTitle?: string | null) {
  const raw = String(unitTitle ?? '').trim()
  if (!raw) return { curriculum: [] as string[], custom: [] as string[] }
  const segments = raw
    .split(/\s*[;|]\s*/)
    .map((part) => part.trim())
    .filter(Boolean)
  const curriculum: string[] = []
  const custom: string[] = []
  for (const segment of segments) {
    if (/custom/i.test(segment)) custom.push(segment.replace(/^custom[:\s-]*/i, '').trim() || segment)
    else curriculum.push(segment)
  }
  if (curriculum.length === 0 && custom.length === 0) curriculum.push(raw)
  return { curriculum, custom }
}

export const FLAG_LABELS: Record<string, string> = {
  homework_assigned: 'Homework assigned',
  quiz_done: 'Quiz / check-in completed',
  materials_missing: 'Materials needed',
}
