( function( $ ) {
	'use strict';

	function getItems( $widget ) {
		return $widget.find( '> .iaew-advanced-nested-accordion__item' );
	}

	function setItemState( $widget, activeIndex, focusSummary ) {
		var maxExpanded = $widget.attr( 'data-max-expanded' ) || 'one';
		var $items = getItems( $widget );

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

		$items.each( function( index ) {
			var isActive = index === activeIndex;
			var $item = $( this );
			var $summary = $item.children( '.iaew-advanced-nested-accordion__summary' );

			if ( isActive ) {
				$item.attr( 'open', 'open' );
				$summary.attr( 'aria-expanded', 'true' );

				if ( focusSummary ) {
					this.open = true;
					$summary.trigger( 'focus' );
				}
			} else if ( 'one' === maxExpanded ) {
				$item.removeAttr( 'open' );
				$summary.attr( 'aria-expanded', 'false' );
			}

			$summary.attr( 'tabindex', isActive ? '0' : '-1' );
		} );
	}

	function bindAccordionEvents( $widget ) {
		getItems( $widget ).each( function( index ) {
			var $item = $( this );
			var $summary = $item.children( '.iaew-advanced-nested-accordion__summary' );

			if ( 'yes' === $summary.attr( 'data-iaew-accordion-editor-bound' ) ) {
				return;
			}

			$summary.attr( 'data-iaew-accordion-editor-bound', 'yes' );

			$summary.on( 'click.iaewAdvancedAccordionEditor', function( event ) {
				event.preventDefault();
				event.stopPropagation();

				if ( $item.attr( 'open' ) && 'multiple' === ( $widget.attr( 'data-max-expanded' ) || 'one' ) ) {
					$item.removeAttr( 'open' );
					$summary.attr( 'aria-expanded', 'false' );
					return;
				}

				setItemState( $widget, index, false );
			} );
		} );
	}

	function registerAdvancedAccordionType() {
		if (
			! window.elementor ||
			! window.$e ||
			! elementor.elementsManager ||
			! elementor.elementsManager.elementTypes ||
			! elementor.elementsManager.registerElementType ||
			! elementor.modules ||
			! elementor.modules.elements ||
			! elementor.modules.elements.types ||
			! elementor.modules.elements.types.NestedElementBase
		) {
			return false;
		}

		if ( elementor.elementsManager.elementTypes['impact_widgets_elementor_advanced-accordion'] ) {
			return true;
		}

		var nestedElementsComponent = $e.components.get( 'nested-elements' );

		if ( ! nestedElementsComponent || ! nestedElementsComponent.exports || ! nestedElementsComponent.exports.NestedView ) {
			return false;
		}

		var NestedElementBase = elementor.modules.elements.types.NestedElementBase;
		var NestedView = nestedElementsComponent.exports.NestedView;

		class AdvancedAccordionView extends NestedView {
			onRender() {
				if ( NestedView.prototype.onRender ) {
					NestedView.prototype.onRender.apply( this, arguments );
				}

				var $widget = this.$el.find( '.iaew-advanced-nested-accordion' ).first();

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

				bindAccordionEvents( $widget );

				if ( 'expanded' === ( $widget.attr( 'data-default-state' ) || 'expanded' ) ) {
					setItemState( $widget, 0, false );
				}
			}

			filter( child, index ) {
				child.attributes.dataIndex = index + 1;
				return true;
			}

			onAddChild( childView ) {
				var $widget = childView._parent.$el.find( '.iaew-advanced-nested-accordion' ).first();
				var index = childView.model.attributes.dataIndex || 1;
				var $targetItem = getItems( $widget ).eq( index - 1 );
				var isInitialLoad = elementor.previewView.isBuffering;

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

				childView.$el
					.addClass( 'iaew-advanced-nested-accordion__content' )
					.attr( {
						role: 'region',
						'aria-labelledby': $targetItem.attr( 'id' ) || '',
					} );

				$targetItem.append( childView.el );

				bindAccordionEvents( $widget );

				if ( ! isInitialLoad ) {
					setItemState( $widget, index - 1, true );
				}
			}
		}

		class AdvancedAccordionElement extends NestedElementBase {
			getType() {
				return 'impact_widgets_elementor_advanced-accordion';
			}

			getView() {
				return AdvancedAccordionView;
			}
		}

		elementor.elementsManager.registerElementType( new AdvancedAccordionElement() );

		return true;
	}

	if ( ! registerAdvancedAccordionType() ) {
		$( window ).on( 'elementor:init', registerAdvancedAccordionType );
		$( window ).on( 'load', function() {
			window.setTimeout( registerAdvancedAccordionType, 0 );
		} );
	}
}( jQuery ) );
