/**
 * Embed/video provider allowlist + URL parsing for the `embed` section.
 *
 * Only YouTube, Vimeo, and Loom are allowed; each source URL is mapped to its
 * privacy-friendly iframe `src`. Parsing is deterministic and pure (no network)
 * so the section validator (cms-core) and the public renderer (sections-react)
 * agree on exactly what is embeddable — an arbitrary URL can never reach an
 * `<iframe>`.
 */
export const EMBED_PROVIDER_LABEL = {
    youtube: 'YouTube',
    vimeo: 'Vimeo',
    loom: 'Loom',
};
function host(u) {
    return u.hostname.replace(/^www\./, '').toLowerCase();
}
function youtubeId(u) {
    const h = host(u);
    if (h === 'youtu.be')
        return u.pathname.slice(1).split('/')[0] || null;
    if (h === 'youtube.com' || h === 'm.youtube.com' || h === 'youtube-nocookie.com') {
        if (u.pathname === '/watch')
            return u.searchParams.get('v');
        const m = u.pathname.match(/^\/(?:embed|shorts|v)\/([^/?#]+)/);
        if (m)
            return m[1] ?? null;
    }
    return null;
}
function vimeoId(u) {
    const h = host(u);
    if (h !== 'vimeo.com' && h !== 'player.vimeo.com')
        return null;
    const m = u.pathname.match(/(?:^|\/)(\d+)(?:$|[/?#])/);
    return m?.[1] ?? null;
}
function loomId(u) {
    if (host(u) !== 'loom.com')
        return null;
    const m = u.pathname.match(/^\/(?:share|embed)\/([0-9a-f]{8,})/i);
    return m?.[1] ?? null;
}
/** True when `id` is a safe id token to interpolate into an embed URL. */
function safeId(id) {
    return !!id && /^[\w-]+$/.test(id);
}
/**
 * Resolve a pasted video URL to a provider + iframe `src`, or `null` when the
 * URL is not an allowed provider (or is malformed).
 */
export function parseEmbed(raw) {
    if (typeof raw !== 'string' || raw.trim() === '')
        return null;
    let u;
    try {
        u = new URL(raw.trim());
    }
    catch {
        return null;
    }
    if (u.protocol !== 'https:' && u.protocol !== 'http:')
        return null;
    const yt = youtubeId(u);
    if (safeId(yt)) {
        return { provider: 'youtube', id: yt, embedUrl: `https://www.youtube-nocookie.com/embed/${yt}` };
    }
    const vimeo = vimeoId(u);
    if (safeId(vimeo)) {
        return { provider: 'vimeo', id: vimeo, embedUrl: `https://player.vimeo.com/video/${vimeo}` };
    }
    const loom = loomId(u);
    if (safeId(loom)) {
        return { provider: 'loom', id: loom, embedUrl: `https://www.loom.com/embed/${loom}` };
    }
    return null;
}
//# sourceMappingURL=embed.js.map