import type { Track } from '../types/index.js'; export class PlayerQueue { private tracks: Track[] = []; private currentIndex = -1; private shuffledOrder: number[] = []; private shuffleCursor = -1; private shuffleEnabled = false; private onChange: ((tracks: readonly Track[]) => void) | undefined; constructor(onChange?: (tracks: readonly Track[]) => void) { this.onChange = onChange; } setTracks(tracks: readonly Track[], currentIndex = tracks.length ? 0 : -1): void { this.tracks = [...tracks]; this.currentIndex = this.clampIndex(currentIndex); this.rebuildShuffle(); this.emitChange(); } add(track: Track): void { this.tracks.push(track); if (this.currentIndex < 0) this.currentIndex = 0; this.rebuildShuffle(); this.emitChange(); } playNext(track: Track): void { const at = this.currentIndex < 0 ? 0 : this.currentIndex + 1; this.tracks.splice(at, 0, track); if (this.currentIndex < 0) this.currentIndex = 0; this.rebuildShuffle(); this.emitChange(); } remove(trackId: string): boolean { const index = this.tracks.findIndex((track) => track.id === trackId); if (index < 0) return false; this.tracks.splice(index, 1); if (!this.tracks.length) this.currentIndex = -1; else if (index < this.currentIndex) this.currentIndex -= 1; else if (this.currentIndex >= this.tracks.length) this.currentIndex = this.tracks.length - 1; this.rebuildShuffle(); this.emitChange(); return true; } move(trackId: string, targetIndex: number): boolean { const from = this.tracks.findIndex((track) => track.id === trackId); if (from < 0) return false; const current = this.current(); const [track] = this.tracks.splice(from, 1); if (!track) return false; const to = Math.max(0, Math.min(targetIndex, this.tracks.length)); this.tracks.splice(to, 0, track); if (current) this.currentIndex = this.tracks.findIndex((item) => item === current); this.rebuildShuffle(); this.emitChange(); return true; } clear(): void { this.tracks = []; this.currentIndex = -1; this.shuffledOrder = []; this.shuffleCursor = -1; this.emitChange(); } getTracks(): readonly Track[] { return this.tracks; } current(): Track | null { return this.currentIndex >= 0 ? this.tracks[this.currentIndex] ?? null : null; } getCurrentIndex(): number { return this.currentIndex; } setCurrentIndex(index: number): Track | null { const next = this.clampIndex(index); this.currentIndex = next; this.syncShuffleCursor(); return this.current(); } setCurrentById(trackId: string): Track | null { const index = this.tracks.findIndex((track) => track.id === trackId); return index >= 0 ? this.setCurrentIndex(index) : null; } setShuffle(enabled: boolean): void { if (this.shuffleEnabled === enabled) return; this.shuffleEnabled = enabled; this.rebuildShuffle(); } isShuffleEnabled(): boolean { return this.shuffleEnabled; } nextIndex(loopPlaylist: boolean): number | null { if (!this.tracks.length) return null; if (this.shuffleEnabled) { if (!this.shuffledOrder.length) this.rebuildShuffle(); if (this.shuffleCursor + 1 < this.shuffledOrder.length) { return this.shuffledOrder[++this.shuffleCursor] ?? null; } if (!loopPlaylist) return null; this.rebuildShuffle(true); return this.shuffledOrder[++this.shuffleCursor] ?? null; } const next = this.currentIndex + 1; if (next < this.tracks.length) return next; return loopPlaylist ? 0 : null; } previousIndex(loopPlaylist: boolean): number | null { if (!this.tracks.length) return null; if (this.shuffleEnabled) { if (this.shuffleCursor > 0) return this.shuffledOrder[--this.shuffleCursor] ?? null; return loopPlaylist ? this.shuffledOrder[this.shuffledOrder.length - 1] ?? null : null; } const previous = this.currentIndex - 1; if (previous >= 0) return previous; return loopPlaylist ? this.tracks.length - 1 : null; } private clampIndex(index: number): number { if (!this.tracks.length) return -1; return Math.max(0, Math.min(index, this.tracks.length - 1)); } private rebuildShuffle(avoidCurrentFirst = false): void { const order = this.tracks.map((_, index) => index); for (let i = order.length - 1; i > 0; i -= 1) { const j = Math.floor(Math.random() * (i + 1)); [order[i], order[j]] = [order[j]!, order[i]!]; } if (this.currentIndex >= 0) { const at = order.indexOf(this.currentIndex); if (at >= 0) { order.splice(at, 1); if (avoidCurrentFirst && order.length) order.push(this.currentIndex); else order.unshift(this.currentIndex); } } this.shuffledOrder = order; this.shuffleCursor = this.currentIndex >= 0 && !avoidCurrentFirst ? 0 : -1; } private syncShuffleCursor(): void { if (!this.shuffleEnabled) return; const index = this.shuffledOrder.indexOf(this.currentIndex); if (index >= 0) this.shuffleCursor = index; else this.rebuildShuffle(); } private emitChange(): void { this.onChange?.(this.tracks); } }