    if (typeof window.videoAPIState === 'undefined') {
      window.videoAPIState = { youtubeAPIReady: false, vimeoAPIReady: false, youtubePlayers: {}, vimeoPlayers: {}, };
    }
    function cleanUpVideoPlayers() {
      Object.values(window.videoAPIState.youtubePlayers).forEach((player) => {
        if (player && typeof player.destroy === 'function') { player.destroy(); }
      });
      window.videoAPIState.youtubePlayers = {};
      Object.values(window.videoAPIState.vimeoPlayers).forEach((player) => {
        if (player && typeof player.unload === 'function') {
          player.unload().then(() => { player.destroy(); })
            .catch((error) => { console.error('Error unloading Vimeo player:', error); });
        }
      });
      window.videoAPIState.vimeoPlayers = {};
    }
    function videoPlayerInitialise(sectionId) {
      const mediaContainer = document.querySelector(sectionId);
      const mainSwiperElement = mediaContainer?.querySelector('.main-swiper');
      if (!mainSwiperElement) { return; }
      const videoPreviewElements = Array.from(mainSwiperElement.getElementsByClassName('video-preview'));
      const iFrameArray = Array.from(mediaContainer.getElementsByClassName('main-slide-video'));
      const videoArray = Array.from(mediaContainer.getElementsByTagName('video'));
      function pauseAllVideos(excludeIframe = null) {
        videoArray.forEach((video) => video.pause());
        Object.values(window.videoAPIState.youtubePlayers).forEach((player) => {
          if (player.getIframe() !== excludeIframe && typeof player.pauseVideo === 'function') { player.pauseVideo(); }
        });
        Object.values(window.videoAPIState.vimeoPlayers).forEach((player) => {
          if (player.element !== excludeIframe) { player.pause(); }
        });
      }
      function onYouTubeIframeAPIReady() {
        iFrameArray.forEach((iframe) => {
          const videoId = iframe.getAttribute('data-video-id');
          if (iframe.src.includes('youtube.com') && !window.videoAPIState.youtubePlayers[videoId]) {
            window.videoAPIState.youtubePlayers[videoId] = new YT.Player(iframe, { events: { onStateChange: onPlayerStateChanges, }, });
          }
        });
      }
      function onPlayerStateChanges(event) {
        if (event.data === YT.PlayerState.PLAYING) {
          pauseAllVideos(event.target.getIframe());
        }
      }
      function loadVimeoAPI(callback) {
        if (window.videoAPIState.vimeoAPIReady) {
          callback();
          return;
        }
        const script = document.createElement('script');
        script.src = 'https://player.vimeo.com/api/player.js';
        script.onload = () => {
          window.videoAPIState.vimeoAPIReady = true;
          callback();
        };
        script.onerror = () => console.error('Failed to load the Vimeo API script.');
        document.head.appendChild(script);
      }
      function setupVimeoPlayers() {
        iFrameArray.forEach((iframe) => {
          const videoId = iframe.getAttribute('data-video-id');
          if (iframe.src.includes('vimeo.com') && !window.videoAPIState.vimeoPlayers[videoId]) {
            const player = new Vimeo.Player(iframe);
            player.on('play', () => { pauseAllVideos(iframe); });
            window.videoAPIState.vimeoPlayers[videoId] = player;
          }
        });
      }
      function toggleVisibility(element, hide) {
        element.classList.toggle('ic-visibility-hidden', hide);
        element.classList.toggle('ic-visibility-visible', !hide);
      }
      function handlePreviewClick(event) {
        const clickedElement = event.target.closest('.video-preview');
        if (!clickedElement) return;
        const nextSiblingElement = clickedElement.nextElementSibling;
        const intVideo = clickedElement.classList.contains('int-video');
        const extVideo = clickedElement.classList.contains('ext-video');
        videoPreviewElements.forEach((preview) => {
          if (preview !== clickedElement) {
            toggleVisibility(preview, false);
            toggleVisibility(preview.nextElementSibling, true);
          }
        });
        toggleVisibility(clickedElement, true);
        pauseAllVideos();
        if (nextSiblingElement) {
          toggleVisibility(nextSiblingElement, false);
          if (intVideo) {
            nextSiblingElement.play();
          } else if (extVideo) {
            const iframe = nextSiblingElement.querySelector('iframe');
            if (iframe) {
              const videoId = iframe.getAttribute('data-video-id');
              if (iframe.src.includes('youtube.com') && window.videoAPIState.youtubePlayers[videoId]) {
                window.videoAPIState.youtubePlayers[videoId].playVideo();
              } else if (iframe.src.includes('vimeo.com') && window.videoAPIState.vimeoPlayers[videoId]) {
                window.videoAPIState.vimeoPlayers[videoId].play();
              }
            }
          }
        }
      }
      mainSwiperElement.addEventListener('click', debounce(handlePreviewClick, 300));
      if (!window.videoAPIState.youtubeAPIReady) {
        loadYouTubeAPI(() => {
          pollYouTubeAPIReady(onYouTubeIframeAPIReady);
        });
      } else {
        onYouTubeIframeAPIReady();
      }
      loadVimeoAPI(setupVimeoPlayers);
    }
    function loadYouTubeAPI(callback) {
      if (window.videoAPIState.youtubeAPIReady) {
        callback();
        return;
      }
      const script = document.createElement('script');
      script.src = 'https://www.youtube.com/iframe_api';
      script.onload = () => {
        window.videoAPIState.youtubeAPIReady = true;
        callback();
      };
      script.onerror = () => console.error('Failed to load the YouTube API script.');
      document.head.appendChild(script);
    }
    function pollYouTubeAPIReady(callback, interval = 100) {
      const checkYTReady = setInterval(() => {
        if (typeof YT !== 'undefined' && YT && YT.Player) {
          clearInterval(checkYTReady);
          callback();
        } else {
          console.log('Waiting for YT.Player to be available...');
        }
      }, interval);
    }
    function debounce(func, wait) {
      let timeout;
      return function (...args) {
        clearTimeout(timeout);
        timeout = setTimeout(() => func.apply(this, args), wait);
      };
    }
    window.onYouTubeIframeAPIReady = () => {
      if (!window.videoAPIState.youtubeAPIReady) {
        window.videoAPIState.youtubeAPIReady = true;
        Object.values(window.videoAPIState.youtubePlayers).forEach((player) => {
          player.pauseVideo();
        });
      }
    };
