import { MOOD_LIST } from './constants.js'

export function formatMoodEmoji(emoji) {
  if (!emoji) return ''
  const item = MOOD_LIST.find(m => m.emoji === emoji)
  return item ? item.emoji : emoji
}

export function getMoodDateRange(range) {
  const now = new Date()
  let startDate = ''
  let endDate = ''

  switch (range) {
    case 'today':
      startDate = formatDate(now)
      endDate = formatDate(now)
      break
    case 'week':
      const weekStart = new Date(now)
      const dayOfWeek = now.getDay() || 7
      weekStart.setDate(now.getDate() - dayOfWeek + 1)
      const weekEnd = new Date(weekStart)
      weekEnd.setDate(weekStart.getDate() + 6)
      startDate = formatDate(weekStart)
      endDate = formatDate(weekEnd)
      break
    case 'month':
      const monthStart = new Date(now.getFullYear(), now.getMonth(), 1)
      const monthEnd = new Date(now.getFullYear(), now.getMonth() + 1, 0)
      startDate = formatDate(monthStart)
      endDate = formatDate(monthEnd)
      break
    case 'all':
    default:
       startDate = formatDate(now)
       endDate = formatDate(now)
      break
  }

  return { startDate, endDate }
}

export function getMoodTypeName(type) {
  const map = {
    happy: '开心',
    neutral: '平静',
    sad: '低落',
    angry: '生气',
    tired: '疲惫',
    anxious: '焦虑',
    excited: '兴奋',
    thinking: '思考',
    sad2: '难过',
    cool: '酷',
    happy2: '幸福',
    angry2: '愤怒',
    celebrate: '庆祝',
    surprised: '惊讶',
    grateful: '感恩',
    cheer: '加油',
    shy: '害羞',
    admire: '崇拜',
    sleepy: '困倦',
    satisfied: '满足'
  }
  return map[type] || type || '未知'
}

export function getMoodLabel(emoji) {
  const item = MOOD_LIST.find(m => m.emoji === emoji)
  return item ? item.label : ''
}

export function getMoodTypeByEmoji(emoji) {
  const item = MOOD_LIST.find(m => m.emoji === emoji)
  if (!item) return 'neutral'
  const label = item.label
  if (['开心', '兴奋', '幸福', '庆祝', '满足'].includes(label)) return 'happy'
  if (['低落', '难过', '焦虑', '困倦'].includes(label)) return 'sad'
  if (['生气', '愤怒'].includes(label)) return 'angry'
  if (['疲惫'].includes(label)) return 'tired'
  return 'neutral'
}

function formatDate(date) {
  const y = date.getFullYear()
  const m = String(date.getMonth() + 1).padStart(2, '0')
  const d = String(date.getDate()).padStart(2, '0')
  return `${y}-${m}-${d}`
}

export default {
  getMoodDateRange,
  getMoodTypeName,
  getMoodLabel,
  getMoodTypeByEmoji
}