// 动态获取icon路径
export const getIconUrl = url => {
  try {
    return require(`../assets/icons/${url}.png`)
  } catch (error) {
    console.error(`无法加载图标: ${url}`, error)
    return ''
  }
}

// 获取本地存储
export const getLocalStorage = key => {
  if (typeof window !== 'undefined') {
    return window.localStorage.getItem(key)
  }
  return null
}

// 设置本地存储
export const setLocalStorage = (key, value) => {
  if (typeof window !== 'undefined') {
    window.localStorage.setItem(key, value)
  }
}

// 格式化日期
export const formatDate = date => {
  if (!date) return ''
  const now = new Date()
  const messageDate = new Date(date)

  // 如果是今天的邮件，只显示时间
  if (messageDate.toDateString() === now.toDateString()) {
    return messageDate.toLocaleTimeString('zh-CN', {
      hour: '2-digit',
      minute: '2-digit',
      second: '2-digit'
    })
  }

  // 如果是昨天的邮件，显示"昨天"加时间
  const yesterday = new Date(now)
  yesterday.setDate(now.getDate() - 1)
  if (messageDate.toDateString() === yesterday.toDateString()) {
    return (
      '昨天 ' +
      messageDate.toLocaleTimeString('zh-CN', {
        hour: '2-digit',
        minute: '2-digit',
        second: '2-digit'
      })
    )
  }

  // 其他情况显示完整日期 (yyyy-MM-dd格式)
  return (
    messageDate.getFullYear() +
    '-' +
    String(messageDate.getMonth() + 1).padStart(2, '0') +
    '-' +
    String(messageDate.getDate()).padStart(2, '0')
  )
}
