// src/utils/imagePreloader.js
import { imageCacheManager } from '@/utils/image/imageCacheManager'

export const preloadImages = async (sources) => {
  let urls = []

  // 支持数组和对象形式的图片源
  if (Array.isArray(sources)) {
    urls = sources
  } else if (typeof sources === 'object') {
    urls = Object.values(sources)
  }

  // 去重：确保每个 URL 只处理一次
  const uniqueUrls = [...new Set(urls)]

  // 创建预加载 Promise 数组
  const preloadPromises = uniqueUrls.map((url) => {
    if (imageCacheManager.isBuildAsset(url)) {
      return imageCacheManager.scheduleRequest(url) // 使用调度器统一管理
    } else {
      return imageCacheManager.scheduleRequest(url)
    }
  })

  // 等待所有预加载完成
  try {
    await Promise.all(preloadPromises)
    console.log('图片预加载完成')
  } catch (error) {
    console.error('预加载图片失败:', error)
    throw error
  }
}
