import { useStoryblokApi } from '@storyblok/astro'; import type { SbBlokData } from '@storyblok/astro'; function getVersion(): 'draft' | 'published' { return import.meta.env.STORYBLOK_IS_PREVIEW === 'true' ? 'draft' : 'published'; } export interface Spectacle { id: string; title: string; category: 'jeune-public' | 'tout-public'; retired: boolean; age: string; duration: string; summary: string; credits: string; image: string; gallery: string[]; dossierPro: string; _blok: SbBlokData; } export interface AgendaEvent { id: string; date: string; location: string; spectacleId: string; bookingLink: string | null; _blok: SbBlokData; } function extractSlug(field: unknown): string { if (!field) return ''; if (typeof field === 'string') return field; if (typeof field === 'object' && field !== null) { const link = field as Record; const fullSlug = link.cached_url || link.slug || ''; return fullSlug.split('/').filter(Boolean).pop() || ''; } return ''; } export function mapStoryToSpectacle(story: any): Spectacle { const c = story.content; return { id: story.slug, title: c.titre || story.name, category: c.category || 'tout-public', retired: c.retire || false, age: c.age || '', duration: c.duree || '', summary: c.resume || '', credits: c.credits || '', image: c.image?.filename || '', gallery: (c.galerie || []).map((a: any) => a.filename), dossierPro: c.dossier?.filename || '', _blok: c, }; } export function mapStoryToEvent(story: any): AgendaEvent { const c = story.content; return { id: story.uuid, date: c.date || '', location: c.lieu || '', spectacleId: extractSlug(c.spectacle_slug), bookingLink: c.lien_reservation || null, _blok: c, }; } export async function fetchSpectacles(): Promise { const api = useStoryblokApi(); const { data } = await api.get('cdn/stories', { content_type: 'spectacle', version: getVersion(), per_page: 100, }); return data.stories.map(mapStoryToSpectacle); } export async function fetchAgenda(): Promise { const api = useStoryblokApi(); const { data } = await api.get('cdn/stories', { content_type: 'evenement', version: getVersion(), per_page: 100, }); return data.stories.map(mapStoryToEvent); }