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

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

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

export const DEFAULT_BANNER: BannerItem = {
  id: 'default',
  linkType: 'ROUTE',
  linkTarget: '/bet',
  translation: {
    title: '',
    imageUrl: defaultBannerImg,
  },
};

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

export function resolveBanners(banners: BannerItem[] | undefined | null): BannerItem[] {
  const fromApi = (banners ?? []).map((banner) => ({
    ...banner,
    translation: {
      ...banner.translation,
      imageUrl: pickImageUrl(banner.translation?.imageUrl),
    },
  }));

  if (fromApi.length > 0) return fromApi;

  const defaultSlide: BannerItem = {
    ...DEFAULT_BANNER,
    translation: {
      ...DEFAULT_BANNER.translation,
      imageUrl: defaultBannerImg || FALLBACK_BANNER_URL,
    },
  };

  return [defaultSlide];
}
