import { filmCaptionsUrl, filmUrl } from './media.config.js';
const playIcon = '';
// Shows a film's poster with a pill-shaped play button; the R2 video loads only on click.
export function mountFilm(container, film, { buttonText = 'Play' } = {}) {
if (!container) return;
container.replaceChildren();
const poster = document.createElement('img');
Object.assign(poster, { src: film.poster, alt: '', loading: 'lazy', decoding: 'async' });
poster.className = 'yt-poster';
const button = document.createElement('button');
button.type = 'button';
button.className = 'yt-play';
button.setAttribute('aria-label', `${buttonText}: ${film.label}`);
button.innerHTML = `${playIcon}`;
button.children[1].textContent = buttonText;
button.children[2].textContent = film.duration;
button.addEventListener('click', () => {
const small = window.matchMedia('(max-width: 820px)').matches || navigator.connection?.saveData;
const video = document.createElement('video');
if (film.captions) video.crossOrigin = 'anonymous';
Object.assign(video, { src: filmUrl(film, small ? '720p' : '1080p'), poster: film.poster, controls: true, autoplay: true, playsInline: true, title: film.label });
video.setAttribute('aria-label', film.label);
if (film.captions) {
const track = document.createElement('track');
Object.assign(track, { kind: 'captions', src: filmCaptionsUrl(film), srclang: 'en', label: 'English' });
video.append(track);
}
container.replaceChildren(video);
container.classList.add('is-playing');
video.play().catch(() => {});
window.gtag?.('event', 'video_start', { video_title: film.label, video_provider: 'r2', video_url: video.src });
});
container.append(poster, button);
container.classList.add('is-ready', 'yt-film');
}