(function ($) {
    'use strict';

	function productsCarousel( $selector ) {
		$selector.addClass('swiper');
		$selector.find('ul.products').addClass('swiper-wrapper');
		$selector.find('ul.products').after('<span class="ecomus-svg-icon em-button-light ecomus-swiper-button swiper-button swiper-button-prev"><svg width="7" height="11" viewBox="0 0 7 11" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M5.5 11L0 5.5L5.5 0L6.47625 0.97625L1.9525 5.5L6.47625 10.0238L5.5 11Z" fill="currentColor"/></svg></span>');
		$selector.find('ul.products').after('<span class="ecomus-svg-icon em-button-light ecomus-swiper-button swiper-button swiper-button-next"><svg width="7" height="11" viewBox="0 0 7 11" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1.5 11L7 5.5L1.5 0L0.52375 0.97625L5.0475 5.5L0.52375 10.0238L1.5 11Z" fill="currentColor"/></svg></span>');
		$selector.find('li.product').addClass('swiper-slide');

		var	options = {
				loop: false,
				autoplay: false,
				speed: 800,
				watchOverflow: true,
				navigation: {
					nextEl: $selector.find('.swiper-button-next:not(.ecomus-product-card-swiper-button)').get(0),
					prevEl: $selector.find('.swiper-button-prev:not(.ecomus-product-card-swiper-button)').get(0),
				},
				breakpoints: {
					0: {
						slidesPerView: 2,
						spaceBetween: 10,
					},
					767: {
						slidesPerView: $selector.data('columns'),
						spaceBetween: 20,
					},

				}
			};

		new Swiper($selector.get(0), options);
	}

	function zIndexFeaturedIcons( $selector ) {
		$selector.on('mouseover', '.product-featured-icons a', function (e) {
			$(this).closest('ul.products').css('z-index', '1001');
		}).on('mouseout', '.product-featured-icons a', function (e) {
            $(this).closest('ul.products').css('z-index', '1');
        });
	}

	function positionTopNavigationArow( $selector ) {
		jQuery( window ).on( 'resize', function () {
			var imageHeight = $selector.find('.product-thumbnail img').outerHeight();

			$selector.find('.swiper-button').css('--em-arrow-top', imageHeight/2 + 'px');
		});
	}

	/**
     * Document ready
     */
    $(function () {
		$( '.menu-item--widget-products-carousel' ).each( function() {
			if( ! $('.menu-item--widget-products-carousel').length ) {
				return;
			}

			productsCarousel($(this).find('.mega-menu-products-carousel'));
			zIndexFeaturedIcons($(this));
			positionTopNavigationArow($(this));
		} );
    });
})(jQuery);