/* episodes.js — season picker + episode cards for a tv title page. episodesSection(details, { season }) →
Season tabs (≤ 7 seasons) or a dropdown; cards show still, "E3 · title", synopsis, runtime, air date and resume progress; click → /watch/tv/:id/:s/:e. */ import { h, runtimeLabel, plural } from '../util.js'; import { img, season as fetchSeason } from '../tmdb.js'; import { resume, keyFor, progressOf } from '../storage.js'; import { watchHref } from './actions.js'; import { icon } from './icons.js'; function episodeCard(show, ep) { const rec = resume.get(keyFor({ type: 'tv', tmdbId: show.id, season: ep.season_number, episode: ep.episode_number })); const p = progressOf(rec); const future = ep.air_date && new Date(ep.air_date) > new Date(); const still = ep.still_path || show.backdrop_path; const imgEl = still ? h('img.card__img', { src: img(still, 'w300'), srcset: `${img(still, 'w300')} 300w, ${img(still, 'w780')} 780w`, sizes: '(max-width: 720px) 92vw, 320px', alt: '', loading: 'lazy', decoding: 'async', class: ep.still_path ? null : 'is-fallback', onload: (e) => e.target.classList.add('is-loaded'), onerror: (e) => e.target.remove(), }) : null; const left = rec && rec.d ? Math.max(0, rec.d - rec.t) : 0; const metaBits = [ future ? `Airs ${new Date(ep.air_date).toLocaleDateString(undefined, { month: 'short', day: 'numeric', year: 'numeric' })}` : null, !future && p > 0.01 && p < 0.95 ? `${runtimeLabel(Math.round(left / 60)) || '1m'} left` : null, !future && p >= 0.95 ? 'Watched' : null, ep.runtime ? runtimeLabel(ep.runtime) : null, ].filter(Boolean); return h('div.card.card--episode', { role: 'listitem', class: [future ? 'is-future' : null, p >= 0.95 ? 'is-watched' : null] }, h('a.card__link', { href: watchHref('tv', show.id, ep.season_number, ep.episode_number), 'aria-label': `Episode ${ep.episode_number}: ${ep.name}` }, h('div.card__media', h('div.card__fallback', h('span', `E${ep.episode_number}`)), imgEl, h('div.card__shade'), h('span.ep__num', `E${ep.episode_number}`), h('span.card__play.card__play--center', icon('play', { size: 22 })), p > 0.01 ? h('div.progress.card__progress', h('i', { style: { width: `${Math.round(p * 100)}%` } })) : null), h('div.ep__body', h('div.ep__title', ep.name || `Episode ${ep.episode_number}`), metaBits.length ? h('div.ep__meta', metaBits.join(' · ')) : null, ep.overview ? h('p.ep__overview', ep.overview) : null))); } export function episodesSection(show, opts = {}) { const seasons = (show.seasons || []).filter((s) => s.season_number > 0 && (s.episode_count > 0 || s.air_date)); if (!seasons.length && show.seasons && show.seasons.length) seasons.push(...show.seasons); const specials = (show.seasons || []).find((s) => s.season_number === 0 && s.episode_count > 0); if (specials && seasons.length && !seasons.includes(specials)) seasons.push(specials); if (!seasons.length) return null; let current = seasons.find((s) => s.season_number === Number(opts.season)) || seasons[0]; const heading = h('h2.section__title'); const list = h('div.episodes__grid', { role: 'list', 'aria-live': 'polite' }); const label = (s) => (s.season_number === 0 ? 'Specials' : `Season ${s.season_number}`); let picker; const tabs = []; if (seasons.length <= 10) { picker = h('div.tabs', { role: 'tablist', 'aria-label': 'Seasons' }, seasons.map((s) => { const t = h('button.tab', { type: 'button', role: 'tab', 'aria-selected': 'false', onclick: () => select(s) }, label(s)); t._season = s; tabs.push(t); return t; })); picker.addEventListener('keydown', (e) => { const i = tabs.indexOf(document.activeElement); if (i < 0 || (e.key !== 'ArrowRight' && e.key !== 'ArrowLeft')) return; e.preventDefault(); const j = (i + (e.key === 'ArrowRight' ? 1 : -1) + tabs.length) % tabs.length; tabs[j].focus(); select(tabs[j]._season); }); } else { const sel = h('select.select', { 'aria-label': 'Season', onchange: (e) => select(seasons.find((s) => String(s.season_number) === e.target.value)) }, seasons.map((s) => h('option', { value: String(s.season_number) }, `${label(s)}${s.episode_count ? ` · ${s.episode_count} ep` : ''}`))); picker = h('div.select-wrap', sel, icon('down', { size: 18 })); picker._sel = sel; } let reqId = 0; async function select(s) { if (!s) return; current = s; tabs.forEach((t) => { const on = t._season === s; t.classList.toggle('is-active', on); t.setAttribute('aria-selected', on ? 'true' : 'false'); t.tabIndex = on ? 0 : -1; }); if (picker._sel) picker._sel.value = String(s.season_number); heading.textContent = s.episode_count ? plural(s.episode_count, 'episode') : label(s); const my = ++reqId; list.replaceChildren(...Array.from({ length: Math.min(s.episode_count || 6, 8) }, () => h('div.card.card--episode.card--skeleton', h('div.card__media.skeleton'), h('div.ep__body', h('div.skeleton.skeleton--line'), h('div.skeleton.skeleton--line.skeleton--short'))))); try { const data = await fetchSeason(show.id, s.season_number); if (my !== reqId) return; const eps = data.episodes || []; heading.textContent = `${label(s)} · ${plural(eps.length, 'episode')}`; list.replaceChildren(...eps.map((ep) => episodeCard(show, ep))); if (!eps.length) list.replaceChildren(h('p.muted', 'No episodes listed yet.')); } catch (e) { if (my !== reqId) return; list.replaceChildren(h('p.muted', `Could not load episodes: ${e.message}`)); } } const section = h('section.section.episodes', { id: 'episodes' }, h('div.section__head', heading, picker), list); section.refresh = () => select(current); select(current); return section; }