const TZ = 'Asia/Shanghai'

// Backend serializes LocalDateTime without timezone (Jackson time-zone: UTC).
// Bare ISO strings like "2026-05-21T14:30:00" are treated as local time by browsers,
// so we append 'Z' to force UTC parsing before converting to Beijing time.
function toUTC(value: string): string {
  return /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}/.test(value) && !/[Z+\-]\d*$/.test(value)
    ? value + 'Z'
    : value
}

export function formatTime(value: string | number | null | undefined): string {
  if (value === null || value === undefined || value === '') return '-'
  const input = typeof value === 'string' ? toUTC(value) : value
  const date = new Date(input)
  return Number.isNaN(date.getTime()) ? '-' : date.toLocaleString('zh-CN', { timeZone: TZ })
}
