import type { BannerItem } from '../components/BannerCarousel.vue';

/** 默认轮播首图：apps/player/src/assets/images/banner.webp */
import defaultBannerImg from '../assets/images/banner.webp';

const FALLBACK_BANNER_URL = '/uploads/banners/welcome.svg';

export const DEFAULT_BANNER: BannerItem = {
  id: 'default',
  translation: {
    title: '',
    imageUrl: defaultBannerImg,
  },
};

function pickImageUrl(url?: string | null): string {
  if (!url) return defaultBannerImg;
  return url;
}

export function resolveBanners(banners: BannerItem[] | undefined | null): BannerItem[] {
  // API 数据尚未到达（加载中），返回空数组避免闪烁默认图
  if (banners == null) return [];

  const fromApi = banners.map((banner) => ({
    ...banner,
    translation: {
      ...banner.translation,
      imageUrl: pickImageUrl(banner.translation?.imageUrl),
    },
  }));

  if (fromApi.length > 0) return fromApi;

  // API 返回了但列表为空，展示默认 Banner
  const defaultSlide: BannerItem = {
    ...DEFAULT_BANNER,
    translation: {
      ...DEFAULT_BANNER.translation,
      imageUrl: defaultBannerImg || FALLBACK_BANNER_URL,
    },
  };

  return [defaultSlide];
}
