// /utils/imageUrl.js - 统一的COS图片路径管理
const app = getApp();

/**
 * 获取完整的COS图片URL
 * @param {string} path - 图片相对路径，如 'a_long/images/common/arrow-down.png'
 * @returns {string} 完整的URL
 */
export function getImageUrl(path) {
  if (!app || !app.globalData || !app.globalData.ossImageUrl) {
    console.warn('⚠️ App.globalData.ossImageUrl 未初始化');
    return path; // 返回原路径作为fallback
  }
  
  // 如果已经是完整URL，直接返回
  if (path.startsWith('http')) {
    return path;
  }
  
  // 拼接完整URL
  return app.globalData.ossImageUrl + path;
}

/**
 * 获取常用图片的URL
 */
export const imageUrls = {
  // 身份相关
  identityIcon: 'a_long/images/identity/identity-icon.png',
  arrowDown: 'a_long/images/common/arrow-down.png',
  bossIcon: 'a_long/images/identity/boss-icon.png',
  dashouIcon: 'a_long/images/identity/dashou-icon.png',
  shangjiaIcon: 'a_long/images/identity/shangjia-icon.png',
  
  // 公共图标
  actionIcon: 'a_long/images/common/action.png',
  emptyMessage: 'a_long/images/common/empty-message.png',
  
  // 通知相关
  notificationOn: 'a_long/images/notifications/notification-on.png',
  notificationOff: 'a_long/images/notifications/notification-off.png',
  
  // TabBar图标
  tabbarHome: 'a_long/images/tabbar/home.png',
  tabbarHomeActive: 'a_long/images/tabbar/home-active.png',
  tabbarCategory: 'a_long/images/tabbar/category.png',
  tabbarCategoryActive: 'a_long/images/tabbar/category-active.png',
  tabbarMessage: 'a_long/images/tabbar/message.png',
  tabbarMessageActive: 'a_long/images/tabbar/message-active.png',
  tabbarProfile: 'a_long/images/tabbar/profile.png',
  tabbarProfileActive: 'a_long/images/tabbar/profile-active.png',
  
  // 用户相关
  defaultAvatar: 'a_long/images/common/default-avatar.png'
};

/**
 * 预加载常用图片
 */
export function preloadCommonImages() {
  const images = Object.values(imageUrls).map(path => getImageUrl(path));
  
  images.forEach(src => {
    wx.getImageInfo({
      src: src,
      success: () => {
        console.log('✅ 图片预加载成功:', src);
      },
      fail: (err) => {
        console.warn('⚠️ 图片预加载失败:', src, err);
      }
    });
  });
}