export default function lightboxVideo() {
   initCustomVideoLightbox(".glightbox-video-hero-case");
   initCustomVideoLightbox(".glightbox-video-hero-case--mobile");
   initCustomVideoLightbox(".glightbox-video-service");
}

function getEmbedUrl(url) {
   let match;

   match = url.match(/(?:youtube\.com\/watch\?v=|youtu\.be\/)([a-zA-Z0-9_-]{11})/);
   if (match) return `https://www.youtube.com/embed/${match[1]}?autoplay=1`;

   match = url.match(/video(-?\d+)_(\d+)/);
   if (match) return `https://vk.com/video_ext.php?oid=${match[1]}&id=${match[2]}&hd=2`;

   match = url.match(/rutube\.ru\/video\/([a-zA-Z0-9]+)/);
   if (match) return `https://rutube.ru/play/embed/${match[1]}/`;

   return url;
}

function initCustomVideoLightbox(selector) {
   document.querySelectorAll(selector).forEach((link) => {
      link.addEventListener("click", function (e) {
         e.preventDefault();

         const embedUrl = getEmbedUrl(this.getAttribute("href"));
         console.log(embedUrl);

         GLightbox({
            touchNavigation: true,
            autoplayVideos: false,

            elements: [{ href: embedUrl }],
         }).open();
      });
   });
}
