export type VideoSource = { kind: "iframe"; src: string } | { kind: "file"; src: string } | null;

const FILE_RE = /\.(mp4|webm|ogg|ogv|mov|m4v|m3u8)(\?|#|$)/i;

export function parseVideo(url: string): VideoSource {
  if (!url) return null;
  const trimmed = url.trim();

  // Direct videobestand of streaming-url → HTML5 <video>
  if (FILE_RE.test(trimmed)) return { kind: "file", src: trimmed };

  // YouTube: watch, youtu.be, shorts, embed, live
  const yt = trimmed.match(/(?:youtube(?:-nocookie)?\.com\/(?:watch\?[^#]*v=|embed\/|shorts\/|live\/)|youtu\.be\/)([\w-]{11})/);
  if (yt) return { kind: "iframe", src: `https://www.youtube-nocookie.com/embed/${yt[1]}?autoplay=1&rel=0` };

  // Vimeo
  const vm = trimmed.match(/vimeo\.com\/(?:video\/)?(\d+)/);
  if (vm) return { kind: "iframe", src: `https://player.vimeo.com/video/${vm[1]}?autoplay=1` };

  // Overige http(s)-links: probeer als direct videobestand
  if (/^https?:\/\//.test(trimmed)) return { kind: "file", src: trimmed };
  return null;
}
