export const fallbackWarningTypes = [
  { label: '供水管网预警', value: 'WATER_PIPE' },
  { label: '排水管网预警', value: 'SEWER_PIPE' },
  { label: '燃气管网预警', value: 'GAS_PIPE' },
  { label: '窨井预警', value: 'MANHOLE' }
]

export const warningLevels = [
  { label: '蓝色预警（IV级）', shortLabel: '蓝色', value: '1', color: '#3b82f6', tagType: 'primary' },
  { label: '黄色预警（III级）', shortLabel: '黄色', value: '2', color: '#eab308', tagType: 'warning' },
  { label: '橙色预警（II级）', shortLabel: '橙色', value: '3', color: '#f97316', tagType: 'warning' },
  { label: '红色预警（I级）', shortLabel: '红色', value: '4', color: '#ef4444', tagType: 'danger' }
]

export const itemStatuses = [
  { label: '已启用', value: 'ENABLED', type: 'success' },
  { label: '已禁用', value: 'DISABLED', type: 'info' },
  { label: '已作废', value: 'INVALID', type: 'danger' }
]

export const industryOptions = ['供水', '排水', '燃气', '窨井']

export const defaultLinks = [
  { name: '监测触发', handler: '监测中心', timeLimit: 5, description: '监测数据达到预警阈值后自动触发' },
  { name: '研判确认', handler: '行业主管部门', timeLimit: 15, description: '核验数据并确认预警事项' },
  { name: '发布处置', handler: '应急处置人员', timeLimit: 30, description: '发布预警并按预案执行处置' },
  { name: '复核归档', handler: '预警管理人员', timeLimit: 60, description: '复核处置结果并完成归档' }
]

export function optionLabel(options, value, fallback = '-') {
  return options.find(item => String(item.value) === String(value))?.label || value || fallback
}

export function statusMeta(value) {
  return itemStatuses.find(item => item.value === value) || { label: value || '未知', type: 'info' }
}

export function levelMeta(value) {
  return warningLevels.find(item => item.value === String(value)) || {
    label: value || '未配置',
    shortLabel: value || '-',
    color: '#94a3b8',
    tagType: 'info'
  }
}

export function parseLinkConfig(value) {
  if (!value) return []
  if (Array.isArray(value)) return value
  try {
    const parsed = JSON.parse(value)
    return Array.isArray(parsed) ? parsed : []
  } catch {
    return [{ name: '环节说明', handler: '-', timeLimit: null, description: value }]
  }
}

export function cloneDefaultLinks() {
  return defaultLinks.map(item => ({ ...item }))
}

export function normalizeWarningTypeDict(data = []) {
  const enabled = data
    .filter(item => item.status === undefined || String(item.status) === '0')
    .map(item => ({ label: item.dictLabel, value: item.dictValue }))
  return enabled.length ? enabled : fallbackWarningTypes
}
