( function( $ ) {
	'use strict';

	function getWidgetElements( $widget ) {
		return {
			tabs: $widget.find( '.iaew-advanced-tabs__tab' ),
			panels: $widget.find( '.iaew-advanced-tabs__content > .iaew-advanced-tabs__panel' ),
		};
	}

	function activateEditorTab( $widget, nextIndex, focusTab ) {
		var elements = getWidgetElements( $widget );
		var $tabs = elements.tabs;
		var $panels = elements.panels;

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

		$tabs.each( function( index ) {
			var isActive = index === nextIndex;
			var $tab = $( this );

			$tab.toggleClass( 'is-active', isActive );
			$tab.attr( 'aria-selected', isActive ? 'true' : 'false' );
			$tab.attr( 'tabindex', isActive ? '0' : '-1' );

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

		$panels.each( function( index ) {
			var isActive = index === nextIndex;
			var $panel = $( this );

			$panel.toggleClass( 'is-active', isActive );

			if ( isActive ) {
				$panel.removeAttr( 'hidden' );
				$panel.css( 'display', '' );
			} else {
				$panel.attr( 'hidden', 'hidden' );
				$panel.css( 'display', 'none' );
			}
		} );

		$widget.attr( 'data-default-tab', nextIndex );
	}

	function bindEditorTabEvents( $widget ) {
		var elements = getWidgetElements( $widget );

		elements.tabs.each( function( index ) {
			var $tab = $( this );

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

			$tab.attr( 'data-iaew-tabs-editor-bound', 'yes' );
			$tab.on( 'click.iaewAdvancedTabsEditor', function( event ) {
				event.preventDefault();
				event.stopPropagation();
				activateEditorTab( $widget, index, false );
			} );
		} );
	}

	function registerAdvancedTabsType() {
		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-tabs'] ) {
			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 AdvancedTabsView extends NestedView {
			onRender() {
				if ( NestedView.prototype.onRender ) {
					NestedView.prototype.onRender.apply( this, arguments );
				}

				var $widget = this.$el.find( '.iaew-advanced-tabs' ).first();
				var defaultIndex = parseInt( $widget.attr( 'data-default-tab' ) || '0', 10 );

				if ( $widget.length ) {
					bindEditorTabEvents( $widget );
					activateEditorTab( $widget, Number.isNaN( defaultIndex ) ? 0 : defaultIndex, false );
				}
			}

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

			onAddChild( childView ) {
				var widget = childView._parent.$el.find( '.iaew-advanced-tabs' )[0];

				if ( ! widget ) {
					return;
				}

				var widgetNumber = widget.dataset.widgetNumber;
				var index = childView.model.attributes.dataIndex;
				var $tab = childView._parent.$el.find( '.iaew-advanced-tabs__tab[data-tab-index="' + ( index - 1 ) + '"]' );
				var tabId = $tab.attr( 'id' );
				var isInitialLoad = elementor.previewView.isBuffering;
				var isActive = isInitialLoad ? 1 === index : $tab.hasClass( 'is-active' );

				childView.$el.attr( {
					id: 'iaew-tab-panel-' + widgetNumber + index,
					role: 'tabpanel',
					'aria-labelledby': tabId,
					'data-tab-index': index - 1,
				} );

				childView.$el.addClass( 'iaew-advanced-tabs__panel' );
				childView.$el.toggleClass( 'is-active', isActive );

				if ( isActive ) {
					childView.$el.removeAttr( 'hidden' );
				} else {
					childView.$el.attr( 'hidden', 'hidden' );
				}

				var $widget = $( widget );

				bindEditorTabEvents( $widget );

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

		class AdvancedTabsElement extends NestedElementBase {
			getType() {
				return 'impact_widgets_elementor_advanced-tabs';
			}

			getView() {
				return AdvancedTabsView;
			}
		}

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

		return true;
	}

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