export interface DownloadEntry {
  id: string;
  contentType: number;
  title: string;
  thumbnail: string;
  url: string;
  downloadedAt: string; // ISO date string
}

const KEY = "doliplay_downloads";

export function getSavedDownloads(): DownloadEntry[] {
  try {
    return JSON.parse(localStorage.getItem(KEY) ?? "[]");
  } catch {
    return [];
  }
}

export function saveDownload(entry: DownloadEntry): void {
  const existing = getSavedDownloads().filter((d) => d.id !== entry.id);
  localStorage.setItem(KEY, JSON.stringify([entry, ...existing]));
}

export function removeDownload(id: string): void {
  const existing = getSavedDownloads().filter((d) => d.id !== id);
  localStorage.setItem(KEY, JSON.stringify(existing));
}

export function clearDownloads(): void {
  localStorage.removeItem(KEY);
}
