function normalizeReason(reason?: string | null): string {
  if (!reason) {
    return ''
  }

  if (reason === 'user has an ongoing session for this event') {
    return '当前活动存在未结束对局'
  }
  if (reason === 'no ongoing session for this event') {
    return '当前活动没有进行中的对局'
  }
  if (reason === 'ready to launch') {
    return '当前可直接开始'
  }
  if (reason === 'launch blocked') {
    return '当前启动受限'
  }

  return reason
}

function normalizeAction(action?: string | null): string {
  if (!action) {
    return '--'
  }

  if (action === 'continue') {
    return '继续上一局'
  }
  if (action === 'launch' || action === 'start') {
    return '开始比赛'
  }
  if (action === 'preview') {
    return '查看活动'
  }

  return action
}

export function formatBackendPlayActionText(action?: string | null, reason?: string | null): string {
  const actionText = normalizeAction(action)
  const reasonText = normalizeReason(reason)
  if (!reasonText) {
    return actionText
  }

  return `${actionText}（${reasonText}）`
}

export function formatBackendPlayStatusText(canLaunch: boolean, action?: string | null, reason?: string | null): string {
  if (!canLaunch) {
    return '当前不可启动'
  }

  if (action === 'continue') {
    return '检测到未结束对局，可继续进入地图'
  }

  const reasonText = normalizeReason(reason)
  if (reasonText) {
    return `${reasonText}，可进入地图`
  }

  return '可启动'
}
