import type { Compilation, Artist, Track } from '~/../types/types' // stores/data.ts import { defineStore } from 'pinia' export const useDataStore = defineStore('data', { state: () => ({ compilations: [] as Compilation[], // Store your compilation data here artists: [] as Artist[], // Store artist data here tracks: [] as Track[], // Store track data here playlistTracks: [] as Track[], // store playslit tracks isLoaded: false // Remember if data is already loaded }), actions: { async loadData() { if (this.isLoaded) return const { data: compilations } = await useFetch('/api/compilations') const { data: artists } = await useFetch('/api/artists') const { data: rawTracks } = await useFetch('/api/tracks') const { data: playlistTracks } = await useFetch('/api/playlists') // Stocker les données de base this.compilations = compilations.value ?? [] this.artists = artists.value ?? [] // Mapper les tracks pour remplacer l'artistId par l'objet Artist const artistMap = new Map(this.artists.map((a) => [a.id, a])) this.tracks = (rawTracks.value ?? []).map((track) => ({ ...track, artist: artistMap.get(track.artist) ?? { id: track.artist, name: 'Unknown', url: '', coverId: '' } })) this.playlistTracks = (playlistTracks.value ?? []).map((track) => ({ ...track, artist: artistMap.get(track.artist) ?? { id: track.artist, name: track.artist, url: '', coverId: '' } })) this.isLoaded = true } }, getters: { // Obtenir tous les compilations getAllCompilations: (state) => state.compilations, getCompilationById: (state) => { return (id: string) => { return state.compilations.find((compilation) => compilation.id === id) } }, // Obtenir toutes les pistes d'une compilation donnée getTracksByCompilationId: (state) => (id: string) => { return state.tracks.filter((track) => track.compilationId === id) }, // Filtrer les artistes selon certains critères getArtistById: (state) => (id: number) => state.artists.find((artist) => artist.id === id), // Obtenir toutes les pistes d'un artiste donné getTracksByArtistId: (state) => (artistId: number) => { return state.tracks.filter((track) => track.artist.id === artistId) }, getFirstTrackOfCompilation() { return (compilationId: string) => { const tracks = this.getTracksByCompilationId(compilationId) return tracks.length > 0 ? tracks[0] : null } }, getFirstTrackOfPlaylist() { return (compilationId: string) => { const tracks = this.getPlaylistTracksByCompilationId(compilationId) return tracks.length > 0 ? tracks[0] : null } }, getNextPlaylistTrack: (state) => { return (track: Track) => { const tracksInPlaylist = state.playlistTracks .filter((t) => t.compilationId === track.compilationId) .sort((a, b) => (a.order ?? 0) - (b.order ?? 0)) const index = tracksInPlaylist.findIndex((t) => t.id === track.id) return index >= 0 && index < tracksInPlaylist.length - 1 ? tracksInPlaylist[index + 1] : null } }, getNextTrack: (state) => { return (track: Track) => { // Récupérer toutes les tracks de la même compilation et les trier par ordre const tracksInCompilation = state.tracks .filter((t) => t.compilationId === track.compilationId) .sort((a, b) => (a.order ?? 0) - (b.order ?? 0)) // Trouver l’index de la track courante const index = tracksInCompilation.findIndex((t) => t.id === track.id) // Retourner la track suivante ou null si c’est la dernière return index >= 0 && index < tracksInCompilation.length - 1 ? tracksInCompilation[index + 1] : null } }, getPrevTrack: (state) => { return (track: Track) => { const tracksInCompilation = state.tracks .filter((t) => t.compilationId === track.compilationId) .sort((a, b) => (a.order ?? 0) - (b.order ?? 0)) const index = tracksInCompilation.findIndex((t) => t.id === track.id) return index > 0 ? tracksInCompilation[index - 1] : null } }, getPlaylistTracksByCompilationId: (state) => (id: string) => { return state.playlistTracks.filter((track) => track.compilationId === id) } } })