(($) => {
  /**
   * Attaches the behavior to evo carousel view.
   */
  Drupal.behaviors.views_evo_carousel = {
    /* eslint no-unused-vars: ["error", { "args": "none" }] */
    attach(context, settings) {
      $(
        '#views-evo-carousel-evo-feature-slider-block-feature-slider',
      ).carousel();

      $('#views-evo-carousel-evo-feature-slider-block-feature-slider').bind(
        'slide.bs.carousel',
        () => {
          // Youtube video stop
          if (
            $('.active iframe[src*="youtu.be"]').length > 0 ||
            $('.active iframe[src*="youtube.com"]').length > 0
          ) {
            let closeIframeYoutube;

            if ($('.active iframe[src*="youtu.be"]').length > 0) {
              closeIframeYoutube = $('.active iframe[src*="youtu.be"]').get(
                0,
              ).contentWindow;
            } else if ($('.active iframe[src*="youtube.com"]').length > 0) {
              closeIframeYoutube = $('.active iframe[src*="youtube.com"]').get(
                0,
              ).contentWindow;
            }
            closeIframeYoutube.postMessage('pause_youtube', '*');
          }

          if ($('.active iframe[src*="vimeo.com"]').length > 0) {
            // Vimeo video stop
            const closeIframeVimeo = $('.active iframe[src*="vimeo.com"]').get(
              0,
            ).contentWindow;
            closeIframeVimeo.postMessage('pause_vimeo', '*');
          }

          // Local Video stop
          $('video').each(() => {
            this.pause();
          });
        },
      );

      $('.indicator').bind('click keypress', (e) => {
        if (e.type !== 'keypress' || e.keyCode === 13) {
          $(
            '#views-evo-carousel-evo-feature-slider-block-feature-slider',
          ).carousel(parseInt($(this).attr('data-slide-to'), 10));
        }
      });

      // Stop or Start carousel autoplay
      $('.btn-carousel-control').on('click', () => {
        if (!$(this).hasClass('paused')) {
          $('.carousel').carousel('pause');
          $('.btn-carousel-control').toggleClass('paused');
          $('.btn-carousel-control i')
            .removeClass('fa-pause')
            .addClass('fa-solid fa-play');
          $(this).blur();
          $('.btn-carousel-control').attr(
            'aria-label',
            'Start Automatic Slide Show',
          );
        } else {
          $('.carousel').carousel('cycle');
          $('.btn-carousel-control').toggleClass('paused');
          $('.btn-carousel-control i')
            .removeClass('fa-play')
            .addClass('fa-solid fa-pause');
          $(this).blur();
          $('.btn-carousel-control').attr(
            'aria-label',
            'Stop Automatic Slide Show',
          );
        }
      });

      // Stop Carousel cycle on hover when carousel control buttons are active
      $('.carousel').hover(
        () => {
          if (!$('.btn-carousel-control').hasClass('paused')) {
            $('.carousel').carousel('pause');
          }
        },
        () => {
          if (!$('.btn-carousel-control').hasClass('paused')) {
            $('.carousel').carousel('cycle');
          }
        },
      );

      // 508 changes to allow the carousel to pause on focus when using keyboard
      $('.carousel').focusin(() => {
        if (!$('.btn-carousel-control').hasClass('paused')) {
          $('.carousel').carousel('pause');
        }
      });

      // 508 change to allow the carousel to resume on focusour when using keyboard
      $('.carousel').focusout(() => {
        if (!$('.btn-carousel-control').hasClass('paused')) {
          $('.carousel').carousel('cycle');
        }
      });
    },
  };
})(jQuery);