import type { AnnouncementStatus } from '@/types/announcement'

const statusNumberMap: Record<number, AnnouncementStatus> = {
  0: 'Draft',
  1: 'Published',
  2: 'Revoked'
}

const statusStringMap: Record<string, AnnouncementStatus> = {
  DRAFT: 'Draft',
  PUBLISHED: 'Published',
  REVOKED: 'Revoked'
}

export const normalizeAnnouncementStatus = (status: unknown): AnnouncementStatus | null => {
  // 1. 数字状态直接映射
  if (typeof status === 'number') {
    return statusNumberMap[status] ?? null
  }

  // 2. 字符串状态兼容大小写与数字
  if (typeof status === 'string') {
    const normalized = status.trim().toUpperCase()
    if (normalized in statusStringMap) {
      return statusStringMap[normalized]
    }

    const numeric = Number(normalized)
    if (Number.isFinite(numeric)) {
      return statusNumberMap[numeric] ?? null
    }
  }

  // 3. 无法识别时返回空
  return null
}
