// 开发者中心 / 平台管理 共用工具：分页提取、枚举中文、脱敏

/** 从后端分页结果中提取列表（兼容 list / records / 纯数组） */
export function getPageList(data: any): any[] {
  if (!data) return []
  if (Array.isArray(data.list)) return data.list
  if (Array.isArray(data.records)) return data.records
  if (Array.isArray(data)) return data
  return []
}

/** 从后端分页结果中提取总数 */
export function getPageTotal(data: any): number {
  if (!data) return 0
  return data.total ?? data.count ?? getPageList(data).length
}

export const APP_TYPE_NAME: Record<number, string> = {
  10: '网站',
  20: '微信小程序',
  30: '抖音小程序',
  40: '百度小程序',
  50: '支付宝小程序',
  60: 'Android',
  70: 'iOS',
  80: 'macOS',
  90: 'Windows',
  100: '插件',
  110: '平台应用',
}

export const PUBLISH_STATUS_NAME: Record<string, string> = {
  developing: '开发中',
  pending_review: '待审核',
  published: '已上架',
  rejected: '未通过',
  deprecated: '已下架',
}

export const STATUS_NAME: Record<number, string> = {
  0: '未开通',
  1: '运行中',
  2: '维护中',
  3: '已关闭',
  4: '已欠费',
  5: '违规停机',
}

export const DOMAIN_STATUS_NAME: Record<number, string> = {
  0: '待解析',
  1: '校验中',
  2: '已生效',
  3: '已拒绝',
  4: '已停用',
}

export const TICKET_STATUS_NAME: Record<string, string> = {
  pending: '待处理',
  assigned: '已分配',
  processing: '处理中',
  resolved: '已解决',
  closed: '已关闭',
  rejected: '已拒绝',
}

export const TICKET_PRIORITY_NAME: Record<string, string> = {
  urgent: '紧急',
  high: '高',
  normal: '普通',
  low: '低',
}

export const TICKET_CATEGORY_NAME: Record<string, string> = {
  bug: '缺陷',
  feature: '需求',
  consultation: '咨询',
  complaint: '投诉',
  other: '其他',
}

/** API Key 脱敏：保留前缀与后4位 */
export function maskKey(key?: string): string {
  if (!key) return ''
  if (key.length <= 12) return key
  return key.slice(0, 8) + '••••••••' + key.slice(-4)
}

/** 颜色映射：根据状态返回主题色 */
export const STATUS_COLORS: Record<string, string> = {
  green: '#16a34a',
  red: '#dc2626',
  amber: '#d97706',
  blue: '#2563eb',
  gray: '#6b7280',
}

export function publishStatusColor(status?: string): string {
  switch (status) {
    case 'published':
      return STATUS_COLORS.green
    case 'pending_review':
      return STATUS_COLORS.amber
    case 'rejected':
      return STATUS_COLORS.red
    default:
      return STATUS_COLORS.gray
  }
}

export function domainStatusColor(status?: number): string {
  switch (status) {
    case 2:
      return STATUS_COLORS.green
    case 0:
    case 1:
      return STATUS_COLORS.amber
    case 3:
    case 4:
      return STATUS_COLORS.red
    default:
      return STATUS_COLORS.gray
  }
}

export function ticketStatusColor(status?: string): string {
  switch (status) {
    case 'resolved':
    case 'closed':
      return STATUS_COLORS.green
    case 'pending':
      return STATUS_COLORS.red
    case 'processing':
    case 'assigned':
      return STATUS_COLORS.blue
    default:
      return STATUS_COLORS.gray
  }
}
