// Audio loader seam. Today: returns the server-relative stream URL
// directly. In the future Tauri shell, the loader can be swapped to
// return blob URLs backed by a Rust-side cache (mirroring the Flutter
// drift+LockCachingAudioSource pattern), without touching the player UI.
//
// Two operations:
// - resolve(track) — returns the URL the <audio> element should src.
// - prefetch(track) — hint that a track will be needed soon. Default
//   no-op (the browser handles it via the prefetch <audio> element);
//   Tauri can warm its native cache here.

import type { TrackRef } from '$lib/api/types';

export type AudioLoader = {
  resolve: (track: TrackRef) => string;
  prefetch?: (track: TrackRef) => void;
};

const defaultLoader: AudioLoader = {
  resolve: (track) => track.stream_url
};

let _current: AudioLoader = defaultLoader;

export const audioLoader: AudioLoader = {
  resolve: (track) => _current.resolve(track),
  prefetch: (track) => _current.prefetch?.(track)
};

/**
 * Replace the active loader. Call once at startup from the Tauri shell;
 * web shell uses the default and never calls this.
 */
export function setAudioLoader(loader: AudioLoader): void {
  _current = loader;
}

/** Test-only: restore the default loader between tests. */
export function __resetAudioLoader(): void {
  _current = defaultLoader;
}
