import type { BasketItem, BasketItemType, BasketSelectedFrom, BasketImportance } from '@/types/basket'

/**
 * Generate a unique dedup key for a basket item.
 * Used in basket store, research store, and QA store for deduplication.
 */
export function getBasketItemKey(item: BasketItem): string {
  switch (item.item_type) {
    case 'document':
      return `document:${item.doc_id}`
    case 'snippet':
      return `snippet:${item.doc_id}:${item.snippet}`
    case 'answer':
      return `answer:${item.question || item.title}:${item.answer}`
    case 'matter':
      return `matter:${item.matter_id}`
    case 'theme':
      return `theme:${item.theme_name}`
  }
}

/**
 * Human-readable label for BasketItemType.
 */
export function getItemTypeLabel(type: BasketItemType | string): string {
  const labels: Record<string, string> = {
    document: '文档',
    snippet: '片段',
    answer: '回答',
    matter: '事项',
    theme: '主题',
    uploaded_file: '上传文件',
  }
  return labels[type] || '资料'
}

/**
 * Human-readable label for BasketSelectedFrom.
 */
export function getSourceLabel(source: BasketSelectedFrom | string): string {
  const labels: Record<string, string> = {
    search: '检索页',
    detail: '详情页',
    qa: '问答页',
    graph: '图谱页',
    matter: '事项页',
    topic: '主题页',
    admin: '后台',
    mock: 'Mock',
    research: 'Research',
    upload: '用户上传',
  }
  return labels[source] || source
}

/**
 * Human-readable label for BasketImportance.
 */
export function getImportanceLabel(importance: BasketImportance): string {
  return importance === 'core' ? '核心参考' : '一般参考'
}

/**
 * Format a timestamp into a short Chinese locale string.
 */
export function formatSessionTime(timestamp: number): string {
  return new Date(timestamp).toLocaleString('zh-CN', {
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
  })
}
