import {
  DOMAIN_LABELS,
  REVIEW_META,
  SCENARIO_LABELS,
  STATUS_META
} from './auditViewMetadata.js'
import { normalizeText } from './auditViewDataUtils.js'

export function makeShort(value) {
  const text = normalizeText(value).replace(/\s+/g, '')
  if (!text) {
    return 'AG'
  }
  return text.slice(0, 2).toUpperCase()
}

export function formatDateTime(value) {
  if (!value) {
    return '未记录'
  }

  const date = new Date(value)
  if (Number.isNaN(date.getTime())) {
    return String(value)
  }

  return new Intl.DateTimeFormat('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    hour12: false
  })
    .format(date)
    .replace(/\//g, '-')
}

export function resolveDomainLabel(value) {
  return DOMAIN_LABELS[value] || normalizeText(value) || '未分类'
}

export function resolveStatusMeta(value) {
  return STATUS_META[value] || { label: normalizeText(value) || '未知状态', tone: 'draft' }
}

export function resolveReviewMeta(value) {
  return REVIEW_META[value] || { label: '暂无审核', tone: 'draft' }
}

export function resolveTimelineEventMeta(value) {
  return {
    created: { label: '创建工作稿', icon: 'mdi mdi-file-document-edit-outline', tone: 'draft' },
    submitted: { label: '提交审核', icon: 'mdi mdi-send-outline', tone: 'warning' },
    approved: { label: '审核通过', icon: 'mdi mdi-check-decagram-outline', tone: 'success' },
    rejected: { label: '审核驳回', icon: 'mdi mdi-close-octagon-outline', tone: 'danger' },
    published: { label: '正式上线', icon: 'mdi mdi-rocket-launch-outline', tone: 'success' },
    restored: { label: '恢复生成工作稿', icon: 'mdi mdi-history', tone: 'info' }
  }[normalizeText(value)] || { label: normalizeText(value) || '版本事件', icon: 'mdi mdi-circle-medium', tone: 'draft' }
}

export function resolveDiffChangeMeta(value) {
  return {
    added: { label: '新增', tone: 'success' },
    removed: { label: '删除', tone: 'danger' },
    modified: { label: '修改', tone: 'warning' }
  }[normalizeText(value)] || { label: normalizeText(value) || '变化', tone: 'draft' }
}

export function formatScenarioList(items) {
  if (!Array.isArray(items) || !items.length) {
    return '未配置场景'
  }

  return items
    .map((item) => SCENARIO_LABELS[item] || item)
    .filter(Boolean)
    .join(' / ')
}

export function resolveTypeKey(assetType) {
  if (assetType === 'rule') {
    return 'rules'
  }
  if (assetType === 'mcp') {
    return 'mcp'
  }
  return ''
}

export function formatSeverity(value) {
  const severity = normalizeText(value).toLowerCase()
  if (severity === 'high') {
    return '高风险'
  }
  if (severity === 'medium') {
    return '中风险'
  }
  if (severity === 'low') {
    return '低风险'
  }
  return '未配置'
}

export function formatInputSummary(items) {
  if (!Array.isArray(items) || !items.length) {
    return '无输入'
  }
  return `${items.length} 项输入`
}

export function formatOutputSummary(items) {
  if (!Array.isArray(items) || !items.length) {
    return '无输出'
  }
  return `${items.length} 项输出`
}

export function formatSpreadsheetChangeSummary(summary) {
  const normalized = normalizeText(summary)
  return (
    normalized
      .replace(/^(ONLYOFFICE\s*)?在线编辑[：:]\s*/i, '')
      .replace(/^ONLYOFFICE\s*在线编辑保存[。.]?\s*/i, '')
      .replace(/^保存表格[：:]\s*/i, '')
      .trim() || '表格内容已保存。'
  )
}
