( function( $ ) {
	'use strict';

	function getAnimationConfig( item ) {
		var widget = item.closest( '.iaew-advanced-nested-accordion' );
		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 resetContentAnimation( content ) {
		content.classList.remove( 'is-animating' );
		content.style.transition = '';
		content.style.height = '';
		content.style.opacity = '';
		content.style.transform = '';
		content.style.overflow = '';
	}

	function animateContent( item, content, expanded ) {
		var config = getAnimationConfig( item );
		var useSlide = config.type === 'slide' || config.type === 'slide-fade';
		var useFade = config.type === 'fade' || config.type === 'slide-fade';
		var startHeight;
		var targetHeight;

		if ( content.dataset.iaewAccordionTimer ) {
			window.clearTimeout( parseInt( content.dataset.iaewAccordionTimer, 10 ) );
			delete content.dataset.iaewAccordionTimer;
		}

		if ( ! config.duration ) {
			resetContentAnimation( content );
			if ( expanded ) {
				content.removeAttribute( 'hidden' );
			} else {
				content.setAttribute( 'hidden', 'hidden' );
			}
			return;
		}

		content.classList.add( 'is-animating' );
		content.style.overflow = 'hidden';
		content.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( ', ' );

		if ( expanded ) {
			content.removeAttribute( 'hidden' );
			targetHeight = content.scrollHeight;
			content.style.height = useSlide ? '0px' : '';
			content.style.opacity = useFade ? '0' : '';
			content.style.transform = useFade ? 'translateY(-8px)' : '';

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

			window.requestAnimationFrame( function() {
				content.style.height = useSlide ? '0px' : '';
				content.style.opacity = useFade ? '0' : '';
				content.style.transform = useFade ? 'translateY(-8px)' : '';
			} );
		}

		content.dataset.iaewAccordionTimer = window.setTimeout( function() {
			resetContentAnimation( content );
			if ( expanded ) {
				content.removeAttribute( 'hidden' );
			} else {
				content.setAttribute( 'hidden', 'hidden' );
			}
			delete content.dataset.iaewAccordionTimer;
		}, config.duration + 30 );
	}

	function setItemExpanded( item, expanded, animate ) {
		var summary = item.querySelector( '.iaew-advanced-nested-accordion__summary' );
		var content = item.querySelector( '.iaew-advanced-nested-accordion__content' );

		item.open = true;
		item.classList.toggle( 'is-open', expanded );

		if ( summary ) {
			summary.setAttribute( 'aria-expanded', expanded ? 'true' : 'false' );
		}

		if ( content ) {
			if ( animate ) {
				animateContent( item, content, expanded );
			} else if ( expanded ) {
				resetContentAnimation( content );
				content.removeAttribute( 'hidden' );
			} else {
				resetContentAnimation( content );
				content.setAttribute( 'hidden', 'hidden' );
			}
		}
	}

	function normalizeInitialState( widget ) {
		var items = widget.querySelectorAll( '.iaew-advanced-nested-accordion__item' );
		var defaultState = widget.getAttribute( 'data-default-state' );
		var maxExpanded = widget.getAttribute( 'data-max-expanded' );

		if ( ! items.length ) {
			return;
		}

		if ( defaultState === 'all_collapsed' ) {
			items.forEach( function( item ) {
				setItemExpanded( item, false );
			} );
			return;
		}

		items.forEach( function( item, index ) {
			var shouldOpen = index === 0;
			if ( shouldOpen ) {
				setItemExpanded( item, true );
			} else if ( maxExpanded !== 'multiple' ) {
				setItemExpanded( item, false );
			} else {
				setItemExpanded( item, item.classList.contains( 'is-open' ) );
			}
		} );
	}

	function initAccordion( widget ) {
		if ( ! widget || widget.dataset.iaewAdvancedNestedAccordionInit === 'yes' ) {
			return;
		}

		widget.dataset.iaewAdvancedNestedAccordionInit = 'yes';
		normalizeInitialState( widget );

		widget.querySelectorAll( '.iaew-advanced-nested-accordion__item' ).forEach( function( item ) {
			var summary = item.querySelector( '.iaew-advanced-nested-accordion__summary' );

			if ( ! summary ) {
				return;
			}

			function handleToggle( event ) {
				event.preventDefault();

				var maxExpanded = widget.getAttribute( 'data-max-expanded' );
				var wasOpen = item.classList.contains( 'is-open' );

				window.setTimeout( function() {
					if ( wasOpen ) {
						setItemExpanded( item, false, true );
						return;
					}

					if ( maxExpanded !== 'multiple' ) {
						widget.querySelectorAll( '.iaew-advanced-nested-accordion__item' ).forEach( function( otherItem ) {
							if ( otherItem !== item ) {
								setItemExpanded( otherItem, false, true );
							}
						} );
					}

					setItemExpanded( item, true, true );
				}, 0 );
			}

			summary.addEventListener( 'click', handleToggle );
		} );
	}

	function initScope( $scope ) {
		$scope.find( '.iaew-advanced-nested-accordion' ).each( function() {
			initAccordion( this );
		} );
	}

	$( window ).on( 'elementor/frontend/init', function() {
		elementorFrontend.hooks.addAction( 'frontend/element_ready/impact_widgets_elementor_advanced-nested-accordion.default', initScope );
		elementorFrontend.hooks.addAction( 'frontend/element_ready/impact_widgets_elementor_advanced-accordion.default', initScope );

		$( function() {
			$( '.elementor-widget-impact_widgets_elementor_advanced-nested-accordion .iaew-advanced-nested-accordion, .elementor-widget-impact_widgets_elementor-advanced-nested-accordion .iaew-advanced-nested-accordion, .elementor-widget-impact_widgets_elementor_advanced-accordion .iaew-advanced-nested-accordion, .elementor-widget-impact_widgets_elementor-advanced-accordion .iaew-advanced-nested-accordion' ).each( function() {
				initAccordion( this );
			} );
		} );
	} );
}( jQuery ) );
