import type { EventAddonCatalogItem } from '../api';

export const scopeDefaults: Record<'photos' | 'gallery' | 'guests', string[]> = {
  photos: ['extra_photos_500', 'extra_photos_2000'],
  gallery: ['extend_gallery_30d', 'extend_gallery_90d'],
  guests: ['extra_guests_300', 'extra_guests_100', 'extra_guests_50', 'extra_guests'],
};

export function selectAddonKeyForScope(addons: EventAddonCatalogItem[], scope: 'photos' | 'gallery' | 'guests'): string {
  const fallback = scope === 'photos' ? 'extra_photos_500' : scope === 'gallery' ? 'extend_gallery_30d' : 'extra_guests';
  const filtered = addons.filter((addon) => addon.price_id && scopeDefaults[scope].includes(addon.key));
  if (filtered.length) {
    return filtered[0].key;
  }
  const scoped = addons.find((addon) => addon.price_id && addon.key.includes(scope));
  return scoped?.key ?? fallback;
}
