function normalizeRawSource(source) {
  return String(source || '').trim()
}

function getBatchIdFromUserScheduledSource(raw) {
  if (!raw.startsWith('user_scheduled')) return ''
  if (!raw.includes(':')) return ''
  return raw.split(':', 2)[1] || ''
}

export function getTaskSourceMeta(source) {
  const raw = normalizeRawSource(source)

  if (!raw || raw === 'manual') {
    return { group: 'manual', label: '手动', type: 'success', tooltip: '' }
  }

  if (raw === 'scheduled') {
    return { group: 'scheduled', label: '定时任务', type: 'primary', tooltip: '系统定时' }
  }

  if (raw.startsWith('user_scheduled')) {
    const batchId = getBatchIdFromUserScheduledSource(raw)
    const batchShort = String(batchId || '').replace(/^batch_/, '')
    return {
      group: 'scheduled',
      label: '定时任务',
      type: 'primary',
      tooltip: batchShort ? `用户定时批次：${batchShort}` : '用户定时',
    }
  }

  const manualTips = {
    batch: '手动批量',
    manual_screenshot: '手动截图',
    immediate: '立即执行',
    resumed: '断点恢复',
  }
  const tip = manualTips[raw] || raw

  return { group: 'manual', label: '手动', type: 'success', tooltip: tip }
}

