( function( $ ) {
	'use strict';

	function initFilters( $scope ) {
		var $widget = $scope.find( '.iaew-logo-showcase[data-layout="filter"]' ).first();

		if ( ! $widget.length ) {
			return;
		}

		var $buttons = $widget.find( '[data-filter-controls] .iaew-logo-showcase__filter-button' );
		var $items = $widget.find( '.iaew-logo-showcase__items .iaew-logo-showcase__item' );

		$buttons.off( 'click.iaewLogoFilter' ).on( 'click.iaewLogoFilter', function() {
			var filter = $( this ).data( 'filter' );

			$buttons.removeClass( 'is-active' );
			$( this ).addClass( 'is-active' );

			$items.each( function() {
				var $item = $( this );
				var category = $item.data( 'category' );
				var show = filter === 'all' || category === filter;

				$item.toggleClass( 'is-hidden', ! show );
			} );
		} );
	}

	function buildDots( $widget, pageCount ) {
		var $dotsWrap = $widget.find( '[data-slider-dots]' );

		if ( ! $dotsWrap.length ) {
			return $();
		}

		$dotsWrap.empty();

		for ( var index = 0; index < pageCount; index++ ) {
			var $dot = $( '<button />', {
				type: 'button',
				class: 'iaew-logo-showcase__dot',
				'data-slide-index': index,
				'aria-label': 'Go to slide ' + ( index + 1 )
			} );

			if ( index === 0 ) {
				$dot.addClass( 'is-active' );
			}

			$dotsWrap.append( $dot );
		}

		return $dotsWrap.find( '.iaew-logo-showcase__dot' );
	}

	function initSlider( $scope ) {
		var $widget = $scope.find( '.iaew-logo-showcase[data-layout="slider"]' ).first();

		if ( ! $widget.length ) {
			return;
		}

		var $viewport = $widget.find( '[data-slider-viewport]' );
		if ( ! $viewport.length ) {
			return;
		}

		var autoplay = $widget.data( 'autoplay' ) === true || $widget.data( 'autoplay' ) === 'true';
		var delay = parseInt( $widget.data( 'autoplay-delay' ), 10 ) || 3500;
		var $prev = $widget.find( '[data-slider-prev]' );
		var $next = $widget.find( '[data-slider-next]' );
		var timer = null;
		var dots = $();
		var resizeNamespace = '.iaewLogoSlider' + ( $scope.data( 'id' ) || Math.floor( Math.random() * 100000 ) );

		function getPageCount() {
			var viewportWidth = $viewport.innerWidth();
			var scrollWidth = $viewport.get( 0 ).scrollWidth;

			if ( ! viewportWidth ) {
				return 1;
			}

			return Math.max( 1, Math.ceil( scrollWidth / viewportWidth ) );
		}

		function getCurrentIndex() {
			var viewportWidth = $viewport.innerWidth();

			if ( ! viewportWidth ) {
				return 0;
			}

			return Math.round( $viewport.scrollLeft() / viewportWidth );
		}

		function syncState() {
			var pageCount = getPageCount();
			var current = Math.min( getCurrentIndex(), pageCount - 1 );

			dots.removeClass( 'is-active' ).eq( current ).addClass( 'is-active' );

			if ( $prev.length ) {
				$prev.prop( 'disabled', current <= 0 );
			}

			if ( $next.length ) {
				$next.prop( 'disabled', current >= pageCount - 1 );
			}
		}

		function scrollToIndex( index ) {
			var viewportWidth = $viewport.innerWidth();
			$viewport.get( 0 ).scrollTo( {
				left: Math.max( 0, index ) * viewportWidth,
				behavior: 'smooth'
			} );
		}

		function bindDots() {
			$widget.find( '.iaew-logo-showcase__dot' ).off( 'click.iaewLogoSlider' ).on( 'click.iaewLogoSlider', function() {
				scrollToIndex( parseInt( $( this ).data( 'slide-index' ), 10 ) || 0 );
			} );
		}

		function startAutoplay() {
			if ( ! autoplay ) {
				return;
			}

			stopAutoplay();
			timer = window.setInterval( function() {
				var pageCount = getPageCount();
				var current = getCurrentIndex();
				var nextIndex = current >= pageCount - 1 ? 0 : current + 1;

				scrollToIndex( nextIndex );
			}, delay );
		}

		function stopAutoplay() {
			if ( timer ) {
				window.clearInterval( timer );
				timer = null;
			}
		}

		dots = buildDots( $widget, getPageCount() );
		bindDots();

		$prev.off( 'click.iaewLogoSlider' ).on( 'click.iaewLogoSlider', function() {
			scrollToIndex( getCurrentIndex() - 1 );
		} );

		$next.off( 'click.iaewLogoSlider' ).on( 'click.iaewLogoSlider', function() {
			scrollToIndex( getCurrentIndex() + 1 );
		} );

		$viewport.off( 'scroll.iaewLogoSlider' ).on( 'scroll.iaewLogoSlider', function() {
			window.requestAnimationFrame( syncState );
		} );

		$widget.off( 'mouseenter.iaewLogoSlider mouseleave.iaewLogoSlider' );
		$widget.on( 'mouseenter.iaewLogoSlider', stopAutoplay );
		$widget.on( 'mouseleave.iaewLogoSlider', startAutoplay );

		$( window ).off( resizeNamespace ).on( resizeNamespace, function() {
			dots = buildDots( $widget, getPageCount() );
			bindDots();
			syncState();
		} );

		syncState();
		startAutoplay();
	}

	function initLogoShowcase( $scope ) {
		initFilters( $scope );
		initSlider( $scope );
	}

	$( window ).on( 'elementor/frontend/init', function() {
		elementorFrontend.hooks.addAction( 'frontend/element_ready/impact_widgets_elementor_logo-showcase.default', initLogoShowcase );
	} );
}( jQuery ) );
