( function( $ ) {
	'use strict';

	function getTabs( widget ) {
		return widget.querySelectorAll( '.iaew-advanced-tabs__tab' );
	}

	function getPanels( widget ) {
		return widget.querySelectorAll( '.iaew-advanced-tabs__content > .iaew-advanced-tabs__panel' );
	}

	function getAnimationConfig( widget ) {
		var type = widget ? widget.getAttribute( 'data-animation' ) : 'slide';
		var duration = widget ? parseInt( widget.getAttribute( 'data-animation-duration' ), 10 ) : 300;
		var easing = widget ? widget.getAttribute( 'data-animation-easing' ) : 'ease';

		if ( [ 'none', 'slide', 'fade', 'slide-fade' ].indexOf( type ) === -1 ) {
			type = 'slide';
		}

		if ( isNaN( duration ) || duration < 0 || type === 'none' ) {
			duration = 0;
		}

		if ( [ 'ease', 'ease-in-out', 'ease-out', 'linear' ].indexOf( easing ) === -1 ) {
			easing = 'ease';
		}

		return {
			type: type,
			duration: duration,
			easing: easing
		};
	}

	function resetPanelAnimation( panel ) {
		panel.classList.remove( 'is-animating' );
		panel.style.transition = '';
		panel.style.height = '';
		panel.style.opacity = '';
		panel.style.transform = '';
		panel.style.overflow = '';
		panel.style.display = '';
	}

	function animatePanelIn( widget, panel ) {
		var config = getAnimationConfig( widget );
		var useSlide = config.type === 'slide' || config.type === 'slide-fade';
		var useFade = config.type === 'fade' || config.type === 'slide-fade';
		var targetHeight;

		if ( panel.dataset.iaewTabsTimer ) {
			window.clearTimeout( parseInt( panel.dataset.iaewTabsTimer, 10 ) );
			delete panel.dataset.iaewTabsTimer;
		}

		resetPanelAnimation( panel );
		panel.removeAttribute( 'hidden' );

		if ( ! config.duration ) {
			return;
		}

		panel.classList.add( 'is-animating' );
		panel.style.overflow = 'hidden';
		panel.style.transition = [
			useSlide ? 'height ' + config.duration + 'ms ' + config.easing : '',
			useFade ? 'opacity ' + config.duration + 'ms ' + config.easing : '',
			useFade ? 'transform ' + config.duration + 'ms ' + config.easing : ''
		].filter( Boolean ).join( ', ' );

		targetHeight = panel.scrollHeight;
		panel.style.height = useSlide ? '0px' : '';
		panel.style.opacity = useFade ? '0' : '';
		panel.style.transform = useFade ? 'translateY(-8px)' : '';

		window.requestAnimationFrame( function() {
			panel.style.height = useSlide ? targetHeight + 'px' : '';
			panel.style.opacity = useFade ? '1' : '';
			panel.style.transform = useFade ? 'translateY(0)' : '';
		} );

		panel.dataset.iaewTabsTimer = window.setTimeout( function() {
			resetPanelAnimation( panel );
			delete panel.dataset.iaewTabsTimer;
		}, config.duration + 30 );
	}

	function activateTab( widget, nextIndex, focusTab, animate ) {
		var tabs = getTabs( widget );
		var panels = getPanels( widget );

		if ( ! tabs.length || ! panels.length ) {
			return;
		}

		tabs.forEach( function( tab, index ) {
			var isActive = index === nextIndex;

			tab.classList.toggle( 'is-active', isActive );
			tab.setAttribute( 'aria-selected', isActive ? 'true' : 'false' );
			tab.setAttribute( 'tabindex', isActive ? '0' : '-1' );

			if ( focusTab && isActive ) {
				tab.focus();
			}
		} );

		panels.forEach( function( panel, index ) {
			var isActive = index === nextIndex;

			panel.classList.toggle( 'is-active', isActive );

			if ( isActive ) {
				if ( animate ) {
					animatePanelIn( widget, panel );
				} else {
					resetPanelAnimation( panel );
					panel.removeAttribute( 'hidden' );
				}
			} else {
				resetPanelAnimation( panel );
				panel.setAttribute( 'hidden', 'hidden' );
			}
		} );
	}

	function bindTabEvents( widget ) {
		var tabs = getTabs( widget );

		tabs.forEach( function( tab, index ) {
			if ( tab.dataset.iaewTabsBound === 'yes' ) {
				return;
			}

			tab.dataset.iaewTabsBound = 'yes';

			tab.addEventListener( 'click', function( event ) {
				event.preventDefault();
				event.stopPropagation();
				if ( tab.classList.contains( 'is-active' ) ) {
					return;
				}
				activateTab( widget, index, false, true );
			} );
		} );
	}

	function initTabsWidget( widget ) {
		var tabs = getTabs( widget );
		var panels = getPanels( widget );
		var defaultIndex = parseInt( widget.getAttribute( 'data-default-tab' ) || '0', 10 );

		if ( ! tabs.length || ! panels.length ) {
			return;
		}

		if ( Number.isNaN( defaultIndex ) || defaultIndex < 0 || defaultIndex >= tabs.length ) {
			defaultIndex = 0;
		}

		activateTab( widget, defaultIndex, false, false );
		bindTabEvents( widget );
	}

	function initScope( $scope ) {
		$scope.find( '.iaew-advanced-tabs' ).each( function() {
			initTabsWidget( this );
		} );
	}

	function boot() {
		if ( 'undefined' === typeof elementorFrontend || ! elementorFrontend.hooks ) {
			return;
		}

		elementorFrontend.hooks.addAction( 'frontend/element_ready/impact_widgets_elementor_advanced-tabs.default', initScope );

		$( function() {
			$( '.elementor-widget-impact_widgets_elementor_advanced-tabs .iaew-advanced-tabs, .elementor-widget-impact_widgets_elementor-advanced-tabs .iaew-advanced-tabs' ).each( function() {
				initTabsWidget( this );
			} );
		} );
	}

	$( window ).on( 'elementor/frontend/init', function() {
		boot();
	} );
}( jQuery ) );
