/**
 * Start Image Carousel widget script
 */

( function( $, elementor ) {

	'use strict';

	var widgetImageCarousel = function( $scope, $ ) {

		var $ImageCarousel = $scope.find( '.asa-image-carousel' );
				
        if ( ! $ImageCarousel.length ) {
            return;
        }

		var $ImageCarouselContainer = $ImageCarousel.find('.swiper-carousel'),
			$settings 		 = $ImageCarousel.data('settings');

		// Add preload settings to prevent image size flash on load
		$settings.preloadImages = true;
		$settings.updateOnImagesReady = true;
		$settings.watchSlidesProgress = true;
		$settings.watchSlidesVisibility = true;

		// Access swiper class
        const Swiper = elementorFrontend.utils.swiper;
        initSwiper();
        
        async function initSwiper() {

			var swiper = await new Swiper($ImageCarouselContainer, $settings);

			// Show carousel after swiper is initialized
			$ImageCarousel.addClass('asa-swiper-initialized');

			if ($settings.pauseOnHover) {
				 $($ImageCarouselContainer).hover(function() {
					(this).swiper.autoplay.stop();
				}, function() {
					(this).swiper.autoplay.start();
				});
			}
		};

	};


	jQuery(window).on('elementor/frontend/init', function() {
		elementorFrontend.hooks.addAction( 'frontend/element_ready/asa-image-carousel.default', widgetImageCarousel );
	});

}( jQuery, window.elementorFrontend ) );

/**
 * End Image Carousel widget script
 */

