// 环境配置
const config = {
  development: {
    baseURL: import.meta.env.VITE_API_BASE_URL || 'https://minio.zrbjr.com/',
    uploadURL: import.meta.env.VITE_UPLOAD_BASE_URL || 'http://localhost:3000/api/upload'
  },
  production: {
    baseURL: 'https://minio.zrbjr.com/',
    uploadURL: import.meta.env.VITE_UPLOAD_BASE_URL || `${window.location.origin}/api/upload`
  }
}

// 获取当前环境
const env = import.meta.env.MODE || 'development'

// 导出当前环境的配置
export default config[env]

// 导出具体配置项
export const { baseURL, uploadURL } = config[env]

// 获取完整的图片URL
export const getImageUrl = (imagePath) => {
  // console.log('getImageUrl called with:', imagePath)
  if (!imagePath) return ''
  if (imagePath.startsWith('http')) return imagePath
  
  // 如果图片路径以/uploads开头，直接返回原路径
  if (imagePath.startsWith('/uploads')) {
    const cleanBaseURL = baseURL.replace(/\/$/, '')
    // console.log('Image starts with /uploads, returning original path:', imagePath)
    return `${imagePath}`
  }
  
  // 在开发环境下，也需要根据路径前缀处理
  if (env === 'development') {
    const cleanBaseURL = baseURL.replace(/\/$/, '')
    const cleanImagePath = imagePath.startsWith('/') ? imagePath : `/${imagePath}`
    const fullUrl = `${cleanBaseURL}${cleanImagePath}`
    // console.log('Development environment, returning:', fullUrl)
    return fullUrl
  }
  
  // 生产环境下使用完整URL
  const cleanBaseURL = baseURL.replace(/\/$/, '')
  const cleanImagePath = imagePath.startsWith('/') ? imagePath : `/${imagePath}`
  const fullUrl = `${cleanBaseURL}${cleanImagePath}`
  
  return fullUrl
}

// 获取上传配置
export const getUploadConfig = () => {
  return {
    action: uploadURL,
    headers: {
      Authorization: `Bearer ${localStorage.getItem('token')}`
    }
  }
}