/**
 * Parse a video URL into an embed descriptor. Supports YouTube and Vimeo.
 * Returns null for URLs we don't recognize (the caller should treat those as
 * raw `<video src>` files). Used by VideoElement renderers to switch between
 * `<video>` and `<iframe>` rendering.
 *
 * Important: embed URLs cannot be captured by the server-side export pipeline
 * (cross-origin iframe content isn't accessible to Puppeteer's `__tick` virtual
 * clock and YouTube's player ignores any external time signal). We surface
 * this as a warning in the properties panel.
 */
const YOUTUBE_RE = /(?:youtube\.com\/(?:watch\?v=|embed\/|shorts\/|live\/)|youtu\.be\/)([A-Za-z0-9_-]{6,16})/;
const VIMEO_RE = /vimeo\.com\/(?:video\/)?(\d{6,12})/;
export function parseEmbedUrl(url, opts = {}) {
    if (!url)
        return null;
    if (url.startsWith('data:'))
        return null;
    const ytMatch = url.match(YOUTUBE_RE);
    if (ytMatch) {
        const id = ytMatch[1];
        // YouTube's embed parameters: muted+autoplay = playsinline-on-mobile-friendly,
        // loop requires both `loop=1` AND `playlist=<same id>` (quirk of YouTube's API).
        const params = [];
        if (opts.autoplay)
            params.push('autoplay=1');
        if (opts.muted || opts.autoplay)
            params.push('mute=1'); // browsers block unmuted autoplay
        if (opts.loop) {
            params.push('loop=1');
            params.push(`playlist=${id}`);
        }
        if (opts.startTime)
            params.push(`start=${Math.round(opts.startTime)}`);
        if (opts.endTime)
            params.push(`end=${Math.round(opts.endTime)}`);
        if (!opts.showControls)
            params.push('controls=0');
        params.push('rel=0', 'modestbranding=1', 'playsinline=1');
        return {
            provider: 'youtube',
            id,
            embedSrc: `https://www.youtube.com/embed/${id}?${params.join('&')}`,
            thumbnail: `https://i.ytimg.com/vi/${id}/maxresdefault.jpg`
        };
    }
    const vimMatch = url.match(VIMEO_RE);
    if (vimMatch) {
        const id = vimMatch[1];
        const params = [];
        if (opts.autoplay)
            params.push('autoplay=1');
        if (opts.muted || opts.autoplay)
            params.push('muted=1');
        if (opts.loop)
            params.push('loop=1');
        if (!opts.showControls)
            params.push('controls=0');
        params.push('byline=0', 'portrait=0', 'title=0');
        return {
            provider: 'vimeo',
            id,
            embedSrc: `https://player.vimeo.com/video/${id}?${params.join('&')}`
        };
    }
    return null;
}
export function isEmbedUrl(url) {
    return parseEmbedUrl(url) !== null;
}
