/* storage.js — resume points, My List and preferences in localStorage. Resume record (key bug-tv.resume.v1, unchanged from v4): { key: "m:603" | "t:1396:s1:e2", type: "movie"|"tv", tmdbId, season?, episode?, show: "Show name", title?: "Episode name", poster: "https://…/w342/…", backdrop?: "https://…/w780/…", still?: "https://…/w300/…", t: seconds played, d: duration seconds, at: epoch ms, provider? } My List item (key bug-tv.mylist.v1): { type, id, title, poster_path, backdrop_path, year, rating, addedAt } Prefs (key bug-tv.prefs.v1): free-form { name: value }. Names used by the UI: subtitleLang ("" = off, "en", …), autoplayNext (bool, default true), volume (0..1), muted. Every mutation dispatches a window event so views can refresh: "bugtv:resume", "bugtv:mylist", "bugtv:prefs" (detail = { key? / item? / name? }) */ import { safe } from './util.js'; const RESUME_KEY = 'bug-tv.resume.v1'; const LIST_KEY = 'bug-tv.mylist.v1'; const PREFS_KEY = 'bug-tv.prefs.v1'; const MAX_RECORDS = 200; function readObj(key) { const parsed = safe(() => JSON.parse(localStorage.getItem(key) || '{}'), {}); return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : {}; } function writeObj(key, obj) { safe(() => localStorage.setItem(key, JSON.stringify(obj))); } function emit(name, detail) { safe(() => window.dispatchEvent(new CustomEvent(name, { detail }))); } export function keyFor({ type, tmdbId, season, episode }) { return type === 'tv' ? `t:${tmdbId}:s${season}:e${episode}` : `m:${tmdbId}`; } export const resume = { KEY: RESUME_KEY, keyFor, get(key) { return readObj(RESUME_KEY)[key] || null; }, save(record) { if (!record || !record.key) return; const all = readObj(RESUME_KEY); all[record.key] = { ...record, at: record.at || Date.now() }; const keys = Object.keys(all).sort((a, b) => (all[a].at || 0) - (all[b].at || 0)); while (keys.length > MAX_RECORDS) delete all[keys.shift()]; writeObj(RESUME_KEY, all); emit('bugtv:resume', { key: record.key }); }, remove(key) { const all = readObj(RESUME_KEY); if (all[key]) { delete all[key]; writeObj(RESUME_KEY, all); emit('bugtv:resume', { key }); } }, // remove every record of a movie/show removeShow(type, tmdbId) { const all = readObj(RESUME_KEY); let changed = false; for (const [k, r] of Object.entries(all)) { if (r && r.type === type && String(r.tmdbId) === String(tmdbId)) { delete all[k]; changed = true; } } if (changed) { writeObj(RESUME_KEY, all); emit('bugtv:resume', {}); } }, clear() { writeObj(RESUME_KEY, {}); emit('bugtv:resume', {}); }, list() { return Object.values(readObj(RESUME_KEY)).filter((r) => r && r.key); }, // records of one movie/show, newest first forShow(type, tmdbId) { return resume .list() .filter((r) => r.type === type && String(r.tmdbId) === String(tmdbId)) .sort((a, b) => (b.at || 0) - (a.at || 0)); }, // one entry per movie/show: the most recently watched record wins latestPerShow() { const byShow = new Map(); for (const rec of resume.list().sort((a, b) => (a.at || 0) - (b.at || 0))) { byShow.set(`${rec.type === 'tv' ? 't' : 'm'}:${rec.tmdbId}`, rec); } return [...byShow.values()].sort((a, b) => (b.at || 0) - (a.at || 0)); }, }; // fraction watched 0..1 for a record (0 when unknown) export function progressOf(rec) { return rec && rec.d > 0 ? Math.min(1, Math.max(0, rec.t / rec.d)) : 0; } export const myList = { KEY: LIST_KEY, _all() { return readObj(LIST_KEY); }, has(type, id) { return !!myList._all()[`${type}:${id}`]; }, // item: { type, id, title, poster_path?, backdrop_path?, year?, rating? } → returns new state toggle(item) { const all = myList._all(); const k = `${item.type}:${item.id}`; let state; if (all[k]) { delete all[k]; state = false; } else { all[k] = { type: item.type, id: item.id, title: item.title || item.name || '', poster_path: item.poster_path || null, backdrop_path: item.backdrop_path || null, year: item.year || '', rating: item.rating || item.vote_average || 0, addedAt: Date.now(), }; state = true; } writeObj(LIST_KEY, all); emit('bugtv:mylist', { type: item.type, id: item.id, state }); return state; }, remove(type, id) { const all = myList._all(); if (all[`${type}:${id}`]) { delete all[`${type}:${id}`]; writeObj(LIST_KEY, all); emit('bugtv:mylist', { type, id, state: false }); } }, // newest first all() { return Object.values(myList._all()).sort((a, b) => (b.addedAt || 0) - (a.addedAt || 0)); }, }; export const prefs = { KEY: PREFS_KEY, get(name, fallback) { const all = readObj(PREFS_KEY); return Object.prototype.hasOwnProperty.call(all, name) ? all[name] : fallback; }, set(name, value) { const all = readObj(PREFS_KEY); if (value === undefined) delete all[name]; else all[name] = value; writeObj(PREFS_KEY, all); emit('bugtv:prefs', { name, value }); }, };