<?php
/**
 * Advanced Tabs widget.
 *
 * @package IMPACT_WIDGETS_ELEMENTOR
 */

namespace ImpactAddons\ImpactWidgetsForElementor\Widgets;

use Elementor\Controls_Manager;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Typography;
use Elementor\Icons_Manager;
use Elementor\Modules\NestedElements\Base\Widget_Nested_Base;
use Elementor\Modules\NestedElements\Controls\Control_Nested_Repeater;
use Elementor\Plugin as Elementor_Plugin;
use Elementor\Repeater;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class Advanced_Tabs extends Widget_Nested_Base {
	protected function is_dynamic_content(): bool {
		return true;
	}

	public function get_name(): string {
		return 'impact_widgets_elementor_advanced-tabs';
	}

	public function get_title(): string {
		return esc_html__( 'Advanced Tabs', 'impact-widgets-for-elementor' );
	}

	public function get_icon(): string {
		return 'eicon-tabs';
	}

	public function get_categories(): array {
		return array( 'impact-widgets-elementor' );
	}

	public function get_keywords(): array {
		return array( 'tabs', 'tab', 'nested', 'container' );
	}

	public function get_style_depends(): array {
		return array( 'impact_widgets_elementor_advanced-tabs' );
	}

	public function get_script_depends(): array {
		return array( 'impact_widgets_elementor_advanced-tabs' );
	}

	public function show_in_panel(): bool {
		return Elementor_Plugin::$instance->experiments->is_feature_active( 'nested-elements', true );
	}

	public function has_widget_inner_wrapper(): bool {
		return ! Elementor_Plugin::$instance->experiments->is_feature_active( 'e_optimized_markup' );
	}

	protected function tab_content_container( int $index ): array {
		return array(
			'elType'   => 'container',
			'settings' => array(
				'_title'        => sprintf(
					/* translators: %d: tab number. */
					__( 'Tab #%d Content', 'impact-widgets-for-elementor' ),
					$index
				),
				'content_width' => 'full',
			),
		);
	}

	protected function get_default_children_elements(): array {
		return array(
			$this->tab_content_container( 1 ),
			$this->tab_content_container( 2 ),
			$this->tab_content_container( 3 ),
		);
	}

	protected function get_default_repeater_title_setting_key(): string {
		return 'tab_title';
	}

	protected function get_default_children_title(): string {
		/* translators: %d: tab number. */
		return esc_html__( 'Tab #%d', 'impact-widgets-for-elementor' );
	}

	protected function get_default_children_placeholder_selector(): string {
		return '.iaew-advanced-tabs__content';
	}

	protected function get_html_wrapper_class(): string {
		return 'elementor-widget-impact_widgets_elementor-advanced-tabs';
	}

	protected function register_controls() {
		$this->register_content_controls();
		$this->register_style_controls();
	}

	private function register_content_controls(): void {
		$this->start_controls_section(
			'section_layout',
			array(
				'label' => esc_html__( 'Layout', 'impact-widgets-for-elementor' ),
			)
		);

		$repeater = new Repeater();

		$repeater->add_control(
			'tab_title',
			array(
				'label'       => esc_html__( 'Title', 'impact-widgets-for-elementor' ),
				'type'        => Controls_Manager::TEXT,
				'default'     => esc_html__( 'Tab Title', 'impact-widgets-for-elementor' ),
				'placeholder' => esc_html__( 'Tab Title', 'impact-widgets-for-elementor' ),
				'label_block' => true,
				'dynamic'     => array(
					'active' => true,
				),
			)
		);

		$repeater->add_control(
			'tab_icon',
			array(
				'label'            => esc_html__( 'Icon', 'impact-widgets-for-elementor' ),
				'type'             => Controls_Manager::ICONS,
				'fa4compatibility' => 'icon',
				'skin'             => 'inline',
				'label_block'      => false,
			)
		);

		$repeater->add_control(
			'element_css_id',
			array(
				'label'   => esc_html__( 'CSS ID', 'impact-widgets-for-elementor' ),
				'type'    => Controls_Manager::TEXT,
				'default' => '',
				'dynamic' => array(
					'active' => true,
				),
			)
		);

		$this->add_control(
			'tabs',
			array(
				'label'       => esc_html__( 'Tabs', 'impact-widgets-for-elementor' ),
				'type'        => Control_Nested_Repeater::CONTROL_TYPE,
				'fields'      => $repeater->get_controls(),
				'default'     => array(
					array(
						'tab_title' => esc_html__( 'Overview', 'impact-widgets-for-elementor' ),
					),
					array(
						'tab_title' => esc_html__( 'Features', 'impact-widgets-for-elementor' ),
					),
					array(
						'tab_title' => esc_html__( 'Details', 'impact-widgets-for-elementor' ),
					),
				),
				'title_field' => '{{{ tab_title }}}',
				'button_text' => esc_html__( 'Add Tab', 'impact-widgets-for-elementor' ),
			)
		);

		$this->add_responsive_control(
			'direction',
			array(
				'label'   => esc_html__( 'Direction', 'impact-widgets-for-elementor' ),
				'type'    => Controls_Manager::CHOOSE,
				'options' => array(
					'top'    => array(
						'title' => esc_html__( 'Top', 'impact-widgets-for-elementor' ),
						'icon'  => 'eicon-v-align-top',
					),
					'bottom' => array(
						'title' => esc_html__( 'Bottom', 'impact-widgets-for-elementor' ),
						'icon'  => 'eicon-v-align-bottom',
					),
					'left'   => array(
						'title' => esc_html__( 'Left', 'impact-widgets-for-elementor' ),
						'icon'  => 'eicon-h-align-left',
					),
					'right'  => array(
						'title' => esc_html__( 'Right', 'impact-widgets-for-elementor' ),
						'icon'  => 'eicon-h-align-right',
					),
				),
				'default' => 'top',
			)
		);

		$this->add_responsive_control(
			'tabs_justify',
			array(
				'label'   => esc_html__( 'Justify', 'impact-widgets-for-elementor' ),
				'type'    => Controls_Manager::CHOOSE,
				'options' => array(
					'flex-start' => array(
						'title' => esc_html__( 'Start', 'impact-widgets-for-elementor' ),
						'icon'  => 'eicon-flex eicon-justify-start-h',
					),
					'center'     => array(
						'title' => esc_html__( 'Center', 'impact-widgets-for-elementor' ),
						'icon'  => 'eicon-flex eicon-justify-center-h',
					),
					'flex-end'   => array(
						'title' => esc_html__( 'End', 'impact-widgets-for-elementor' ),
						'icon'  => 'eicon-flex eicon-justify-end-h',
					),
				),
				'default'   => 'flex-start',
				'selectors' => array(
					'{{WRAPPER}} .iaew-advanced-tabs__nav' => 'justify-content: {{VALUE}};',
				),
			)
		);

		$this->add_responsive_control(
			'align_title',
			array(
				'label'   => esc_html__( 'Align Title', 'impact-widgets-for-elementor' ),
				'type'    => Controls_Manager::CHOOSE,
				'options' => array(
					'left'   => array(
						'title' => esc_html__( 'Left', 'impact-widgets-for-elementor' ),
						'icon'  => 'eicon-text-align-left',
					),
					'center' => array(
						'title' => esc_html__( 'Center', 'impact-widgets-for-elementor' ),
						'icon'  => 'eicon-text-align-center',
					),
					'right'  => array(
						'title' => esc_html__( 'Right', 'impact-widgets-for-elementor' ),
						'icon'  => 'eicon-text-align-right',
					),
				),
				'default'   => 'center',
				'selectors' => array(
					'{{WRAPPER}} .iaew-advanced-tabs__tab' => 'text-align: {{VALUE}};',
				),
			)
		);

		$this->add_control(
			'icon_position',
			array(
				'label'   => esc_html__( 'Icon Position', 'impact-widgets-for-elementor' ),
				'type'    => Controls_Manager::SELECT,
				'default' => 'left',
				'options' => array(
					'left'  => esc_html__( 'Left', 'impact-widgets-for-elementor' ),
					'right' => esc_html__( 'Right', 'impact-widgets-for-elementor' ),
					'top'   => esc_html__( 'Top', 'impact-widgets-for-elementor' ),
				),
			)
		);

		$this->add_control(
			'default_active_tab',
			array(
				'label'              => esc_html__( 'Default Active Tab', 'impact-widgets-for-elementor' ),
				'type'               => Controls_Manager::NUMBER,
				'min'                => 1,
				'step'               => 1,
				'default'            => 1,
				'frontend_available' => true,
			)
		);

		$this->add_control(
			'tabs_animation',
			array(
				'label'              => esc_html__( 'Animation', 'impact-widgets-for-elementor' ),
				'type'               => Controls_Manager::SELECT,
				'options'            => array(
					'none'       => esc_html__( 'None', 'impact-widgets-for-elementor' ),
					'slide'      => esc_html__( 'Slide', 'impact-widgets-for-elementor' ),
					'fade'       => esc_html__( 'Fade', 'impact-widgets-for-elementor' ),
					'slide-fade' => esc_html__( 'Slide + Fade', 'impact-widgets-for-elementor' ),
				),
				'default'            => 'slide',
				'frontend_available' => true,
			)
		);

		$this->add_control(
			'tabs_animation_duration',
			array(
				'label'              => esc_html__( 'Animation Duration', 'impact-widgets-for-elementor' ),
				'type'               => Controls_Manager::SLIDER,
				'size_units'         => array( 'ms' ),
				'range'              => array(
					'ms' => array(
						'min'  => 100,
						'max'  => 1200,
						'step' => 50,
					),
				),
				'default'            => array(
					'size' => 300,
					'unit' => 'ms',
				),
				'selectors'          => array(
					'{{WRAPPER}} .iaew-advanced-tabs' => '--iaew-tabs-animation-duration: {{SIZE}}ms;',
				),
				'condition'          => array(
					'tabs_animation!' => 'none',
				),
				'frontend_available' => true,
			)
		);

		$this->add_control(
			'tabs_animation_easing',
			array(
				'label'              => esc_html__( 'Animation Easing', 'impact-widgets-for-elementor' ),
				'type'               => Controls_Manager::SELECT,
				'options'            => array(
					'ease'        => esc_html__( 'Ease', 'impact-widgets-for-elementor' ),
					'ease-in-out' => esc_html__( 'Ease In Out', 'impact-widgets-for-elementor' ),
					'ease-out'    => esc_html__( 'Ease Out', 'impact-widgets-for-elementor' ),
					'linear'      => esc_html__( 'Linear', 'impact-widgets-for-elementor' ),
				),
				'default'            => 'ease',
				'selectors'          => array(
					'{{WRAPPER}} .iaew-advanced-tabs' => '--iaew-tabs-animation-easing: {{VALUE}};',
				),
				'condition'          => array(
					'tabs_animation!' => 'none',
				),
				'frontend_available' => true,
			)
		);

		$this->end_controls_section();
	}

	private function register_style_controls(): void {
		$this->start_controls_section(
			'section_style_nav',
			array(
				'label' => esc_html__( 'Tabs Navigation', 'impact-widgets-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_responsive_control(
			'tabs_gap',
			array(
				'label'      => esc_html__( 'Gap Between Tabs', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array(
					'px' => array(
						'min' => 0,
						'max' => 60,
					),
				),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-advanced-tabs__nav' => 'gap: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'tabs_wrapper_gap',
			array(
				'label'      => esc_html__( 'Navigation / Content Gap', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array(
					'px' => array(
						'min' => 0,
						'max' => 120,
					),
				),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-advanced-tabs' => '--iaew-tabs-gap: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'tabs_nav_width',
			array(
				'label'      => esc_html__( 'Vertical Navigation Width', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%' ),
				'range'      => array(
					'px' => array(
						'min' => 120,
						'max' => 600,
					),
					'%'  => array(
						'min' => 10,
						'max' => 60,
					),
				),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-advanced-tabs' => '--iaew-tabs-nav-width: {{SIZE}}{{UNIT}};',
				),
				'condition'  => array(
					'direction' => array( 'left', 'right' ),
				),
			)
		);

		$this->end_controls_section();

		$this->start_controls_section(
			'section_style_tab_item',
			array(
				'label' => esc_html__( 'Tab Item', 'impact-widgets-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_group_control(
			Group_Control_Typography::get_type(),
			array(
				'name'     => 'tab_title_typography',
				'selector' => '{{WRAPPER}} .iaew-advanced-tabs__tab',
			)
		);

		$this->add_responsive_control(
			'tab_alignment',
			array(
				'label'     => esc_html__( 'Content Alignment', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'flex-start' => array(
						'title' => esc_html__( 'Start', 'impact-widgets-for-elementor' ),
						'icon'  => 'eicon-h-align-left',
					),
					'center'     => array(
						'title' => esc_html__( 'Center', 'impact-widgets-for-elementor' ),
						'icon'  => 'eicon-h-align-center',
					),
					'flex-end'   => array(
						'title' => esc_html__( 'End', 'impact-widgets-for-elementor' ),
						'icon'  => 'eicon-h-align-right',
					),
				),
				'selectors' => array(
					'{{WRAPPER}} .iaew-advanced-tabs__tab' => 'justify-content: {{VALUE}};',
				),
			)
		);

		$this->add_responsive_control(
			'tab_icon_gap',
			array(
				'label'      => esc_html__( 'Icon Gap', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px' ),
				'range'      => array(
					'px' => array(
						'min' => 0,
						'max' => 40,
					),
				),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-advanced-tabs' => '--iaew-tabs-icon-gap: {{SIZE}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'tab_padding',
			array(
				'label'      => esc_html__( 'Padding', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em', 'rem' ),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-advanced-tabs__tab' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'tab_border_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em', 'rem' ),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-advanced-tabs__tab' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->start_controls_tabs( 'tabs_item_states' );

		$this->start_controls_tab(
			'tabs_item_state_normal',
			array(
				'label' => esc_html__( 'Normal', 'impact-widgets-for-elementor' ),
			)
		);

		$this->add_control(
			'tab_text_color',
			array(
				'label'     => esc_html__( 'Text Color', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .iaew-advanced-tabs__tab' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'tab_background',
				'types'    => array( 'classic', 'gradient' ),
				'selector' => '{{WRAPPER}} .iaew-advanced-tabs__tab',
			)
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'tab_border',
				'selector' => '{{WRAPPER}} .iaew-advanced-tabs__tab',
			)
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'tab_box_shadow',
				'selector' => '{{WRAPPER}} .iaew-advanced-tabs__tab',
			)
		);

		$this->end_controls_tab();

		$this->start_controls_tab(
			'tabs_item_state_active',
			array(
				'label' => esc_html__( 'Active', 'impact-widgets-for-elementor' ),
			)
		);

		$this->add_control(
			'tab_active_text_color',
			array(
				'label'     => esc_html__( 'Text Color', 'impact-widgets-for-elementor' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => array(
					'{{WRAPPER}} .iaew-advanced-tabs__tab.is-active' => 'color: {{VALUE}};',
				),
			)
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'tab_active_background',
				'types'    => array( 'classic', 'gradient' ),
				'selector' => '{{WRAPPER}} .iaew-advanced-tabs__tab.is-active',
			)
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'tab_active_border',
				'selector' => '{{WRAPPER}} .iaew-advanced-tabs__tab.is-active',
			)
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'tab_active_box_shadow',
				'selector' => '{{WRAPPER}} .iaew-advanced-tabs__tab.is-active',
			)
		);

		$this->end_controls_tab();

		$this->end_controls_tabs();

		$this->end_controls_section();

		$this->start_controls_section(
			'section_style_content_panel',
			array(
				'label' => esc_html__( 'Content Panel', 'impact-widgets-for-elementor' ),
				'tab'   => Controls_Manager::TAB_STYLE,
			)
		);

		$this->add_group_control(
			Group_Control_Background::get_type(),
			array(
				'name'     => 'panel_background',
				'types'    => array( 'classic', 'gradient' ),
				'selector' => '{{WRAPPER}} .iaew-advanced-tabs__content > .iaew-advanced-tabs__panel',
			)
		);

		$this->add_group_control(
			Group_Control_Border::get_type(),
			array(
				'name'     => 'panel_border',
				'selector' => '{{WRAPPER}} .iaew-advanced-tabs__content > .iaew-advanced-tabs__panel',
			)
		);

		$this->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			array(
				'name'     => 'panel_box_shadow',
				'selector' => '{{WRAPPER}} .iaew-advanced-tabs__content > .iaew-advanced-tabs__panel',
			)
		);

		$this->add_responsive_control(
			'panel_padding',
			array(
				'label'      => esc_html__( 'Padding', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em', 'rem' ),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-advanced-tabs__content > .iaew-advanced-tabs__panel' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->add_responsive_control(
			'panel_border_radius',
			array(
				'label'      => esc_html__( 'Border Radius', 'impact-widgets-for-elementor' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => array( 'px', '%', 'em', 'rem' ),
				'selectors'  => array(
					'{{WRAPPER}} .iaew-advanced-tabs__content > .iaew-advanced-tabs__panel' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				),
			)
		);

		$this->end_controls_section();
	}

	protected function get_initial_config(): array {
		return array_merge(
			parent::get_initial_config(),
			array(
				'support_improved_repeaters' => true,
				'target_container'           => array( '.iaew-advanced-tabs__nav' ),
				'node'                       => 'button',
			)
		);
	}

	protected function render(): void {
		$settings      = $this->get_settings_for_display();
		$tabs          = is_array( $settings['tabs'] ?? null ) ? $settings['tabs'] : array();
		$widget_number = $this->get_id_int();
		$active_index  = max( 0, (int) ( $settings['default_active_tab'] ?? 1 ) - 1 );
		$direction     = $settings['direction'] ?? 'top';
		$icon_position = $settings['icon_position'] ?? 'left';
		$is_vertical   = in_array( $direction, array( 'left', 'right' ), true );
		$animation     = isset( $settings['tabs_animation'] ) ? sanitize_key( $settings['tabs_animation'] ) : 'slide';
		if ( ! in_array( $animation, array( 'none', 'slide', 'fade', 'slide-fade' ), true ) ) {
			$animation = 'slide';
		}
		$animation_duration = 300;
		if ( ! empty( $settings['tabs_animation_duration']['size'] ) ) {
			$animation_duration = max( 0, (int) $settings['tabs_animation_duration']['size'] );
		}
		$animation_easing = isset( $settings['tabs_animation_easing'] ) ? sanitize_key( $settings['tabs_animation_easing'] ) : 'ease';
		if ( ! in_array( $animation_easing, array( 'ease', 'ease-in-out', 'ease-out', 'linear' ), true ) ) {
			$animation_easing = 'ease';
		}

		if ( empty( $tabs ) ) {
			return;
		}

		$this->enqueue_widget_assets();
		$this->enqueue_icon_assets();

		if ( $active_index >= count( $tabs ) ) {
			$active_index = 0;
		}

		$this->add_render_attribute(
			'wrapper',
			array(
				'class'              => array(
					'iaew-advanced-tabs',
					'iaew-advanced-tabs--position-' . $direction,
					$is_vertical ? 'iaew-advanced-tabs--vertical' : 'iaew-advanced-tabs--horizontal',
					'iaew-advanced-tabs--icon-' . $icon_position,
				),
				'data-widget-number' => $widget_number,
				'data-default-tab'   => $active_index,
				'data-animation'     => $animation,
				'data-animation-duration' => (string) $animation_duration,
				'data-animation-easing' => $animation_easing,
				'style'              => '--iaew-tabs-animation-duration:' . esc_attr( $animation_duration ) . 'ms;--iaew-tabs-animation-easing:' . esc_attr( $animation_easing ) . ';',
			)
		);
		?>
		<div <?php $this->print_render_attribute_string( 'wrapper' ); ?>>
			<div class="iaew-advanced-tabs__nav" role="tablist">
				<?php foreach ( $tabs as $index => $item ) : ?>
					<?php $this->render_tab_button( $item, $index, $widget_number, $index === $active_index ); ?>
				<?php endforeach; ?>
			</div>
			<div class="iaew-advanced-tabs__content">
				<?php foreach ( $tabs as $index => $item ) : ?>
					<?php $this->print_child( $index, $item, $widget_number, $index === $active_index ); ?>
				<?php endforeach; ?>
			</div>
		</div>
		<?php
	}

	private function render_tab_button( array $item, int $index, int $widget_number, bool $is_active ): void {
		$tab_count = $index + 1;
		$tab_title = $item['tab_title'] ?? sprintf( __( 'Tab #%d', 'impact-widgets-for-elementor' ), $tab_count );
		$tab_id    = ! empty( $item['element_css_id'] ) ? $item['element_css_id'] : 'iaew-tab-title-' . $widget_number . $tab_count;
		$panel_id  = 'iaew-tab-panel-' . $widget_number . $tab_count;

		$this->add_render_attribute(
			'tab-button-' . $tab_count,
			array(
				'id'            => $tab_id,
				'class'         => array( 'iaew-advanced-tabs__tab', $is_active ? 'is-active' : '' ),
				'type'          => 'button',
				'role'          => 'tab',
				'aria-selected' => $is_active ? 'true' : 'false',
				'tabindex'      => $is_active ? '0' : '-1',
				'aria-controls' => $panel_id,
				'data-tab-index'=> $index,
			)
		);
		?>
		<button <?php $this->print_render_attribute_string( 'tab-button-' . $tab_count ); ?>>
			<?php if ( ! empty( $item['tab_icon']['value'] ) ) : ?>
				<span class="iaew-advanced-tabs__tab-icon" aria-hidden="true">
					<?php Icons_Manager::render_icon( $item['tab_icon'], array( 'aria-hidden' => 'true' ) ); ?>
				</span>
			<?php endif; ?>
			<span class="iaew-advanced-tabs__tab-title"><?php echo esc_html( $tab_title ); ?></span>
		</button>
		<?php
	}

	public function print_child( $index, $item = array(), $widget_number = 0, $is_active = false ) {
		$children  = $this->get_children();
		$child_ids = array();

		foreach ( $children as $child ) {
			$child_ids[] = $child->get_id();
		}

		$add_attribute_to_container = function ( $should_render, $container ) use ( $child_ids, $index, $item, $widget_number, $is_active ) {
			if ( in_array( $container->get_id(), $child_ids, true ) ) {
				$this->add_attributes_to_container( $container, $index, $item, $widget_number, $is_active );
			}

			return $should_render;
		};

		add_filter( 'elementor/frontend/container/should_render', $add_attribute_to_container, 10, 3 );

		if ( isset( $children[ $index ] ) ) {
			$children[ $index ]->print_element();
		}

		remove_filter( 'elementor/frontend/container/should_render', $add_attribute_to_container );
	}

	protected function add_attributes_to_container( $container, int $index, array $item, int $widget_number, bool $is_active ): void {
		$tab_count = $index + 1;
		$tab_id    = ! empty( $item['element_css_id'] ) ? $item['element_css_id'] : 'iaew-tab-title-' . $widget_number . $tab_count;

		$attributes = array(
			'id'             => 'iaew-tab-panel-' . $widget_number . $tab_count,
			'class'          => array( 'iaew-advanced-tabs__panel', $is_active ? 'is-active' : '' ),
			'role'           => 'tabpanel',
			'aria-labelledby'=> $tab_id,
			'data-tab-index' => $index,
		);

		if ( ! $is_active ) {
			$attributes['hidden'] = 'hidden';
		}

		$container->add_render_attribute( '_wrapper', $attributes );
	}

	protected function content_template_single_repeater_item() {
		?>
		<#
		const tabIndex = view.collection.length;
		const tabCount = tabIndex + 1;
		const elementUid = view.getIDInt().toString();
		const tabId = data.element_css_id ? data.element_css_id : 'iaew-tab-title-' + elementUid + tabCount;
		const panelId = 'iaew-tab-panel-' + elementUid + tabCount;
		const tabKey = 'tab-button-' + tabCount;
		const titleKey = 'tab-title-' + tabCount;
		const iconKey = 'tab-icon-' + tabCount;
		const tabIcon = data.tab_icon && data.tab_icon.value ? elementor.helpers.renderIcon( view, data.tab_icon, { 'aria-hidden': true }, 'i', 'object' ) : null;

		view.addRenderAttribute( tabKey, {
			'id': tabId,
			'class': [ 'iaew-advanced-tabs__tab', 1 === tabCount ? 'is-active' : '' ],
			'type': 'button',
			'role': 'tab',
			'aria-selected': 1 === tabCount ? 'true' : 'false',
			'tabindex': 1 === tabCount ? '0' : '-1',
			'aria-controls': panelId,
			'data-tab-index': tabIndex,
		}, null, true );

		view.addRenderAttribute( titleKey, {
			'class': [ 'iaew-advanced-tabs__tab-title' ],
			'data-binding-type': 'repeater-item',
			'data-binding-repeater-name': 'tabs',
			'data-binding-setting': [ 'tab_title', 'element_css_id' ],
			'data-binding-index': tabCount,
			'data-binding-config': JSON.stringify( {
				'element_css_id': {
					editType: 'attribute',
					attr: 'id',
					selector: 'button',
				},
				'tab_title': {
					editType: 'text',
				},
			} ),
		}, null, true );

		view.addRenderAttribute( iconKey, {
			'class': [ 'iaew-advanced-tabs__tab-icon' ],
			'data-binding-type': 'repeater-item',
			'data-binding-repeater-name': 'tabs',
			'data-binding-index': tabCount,
		}, null, true );
		#>
		<button {{{ view.getRenderAttributeString( tabKey ) }}}>
			<# if ( tabIcon && tabIcon.value ) { #>
				<span {{{ view.getRenderAttributeString( iconKey ) }}}>{{{ tabIcon.value }}}</span>
			<# } #>
			<span {{{ view.getRenderAttributeString( titleKey ) }}}>{{{ data.tab_title }}}</span>
		</button>
		<?php
	}

	protected function content_template() {
		?>
		<#
		const tabs = settings.tabs || [];
		const elementUid = view.getIDInt().toString();
		const direction = settings.direction || 'top';
		const iconPosition = settings.icon_position || 'left';
		const activeIndex = Math.max( 0, parseInt( settings.default_active_tab || 1, 10 ) - 1 );
		const orientationClass = [ 'left', 'right' ].includes( direction ) ? 'iaew-advanced-tabs--vertical' : 'iaew-advanced-tabs--horizontal';
		#>
		<div class="iaew-advanced-tabs iaew-advanced-tabs--position-{{ direction }} {{ orientationClass }} iaew-advanced-tabs--icon-{{ iconPosition }}" data-widget-number="{{ elementUid }}" data-default-tab="{{ activeIndex }}" data-animation="{{ settings.tabs_animation || 'slide' }}" data-animation-duration="{{ settings.tabs_animation_duration && settings.tabs_animation_duration.size ? settings.tabs_animation_duration.size : 300 }}" data-animation-easing="{{ settings.tabs_animation_easing || 'ease' }}">
			<# if ( tabs.length ) { #>
				<div class="iaew-advanced-tabs__nav" role="tablist">
					<# _.each( tabs, function( item, index ) {
						const tabCount = index + 1;
						const tabId = item.element_css_id ? item.element_css_id : 'iaew-tab-title-' + elementUid + tabCount;
						const panelId = 'iaew-tab-panel-' + elementUid + tabCount;
						const tabKey = 'tab-button-' + tabCount;
						const titleKey = 'tab-title-' + tabCount;
						const iconKey = 'tab-icon-' + tabCount;
						const tabIcon = item.tab_icon && item.tab_icon.value ? elementor.helpers.renderIcon( view, item.tab_icon, { 'aria-hidden': true }, 'i', 'object' ) : null;

						view.addRenderAttribute( tabKey, {
							'id': tabId,
							'class': [ 'iaew-advanced-tabs__tab', index === activeIndex ? 'is-active' : '' ],
							'type': 'button',
							'role': 'tab',
							'aria-selected': index === activeIndex ? 'true' : 'false',
							'tabindex': index === activeIndex ? '0' : '-1',
							'aria-controls': panelId,
							'data-tab-index': index,
						}, null, true );

						view.addRenderAttribute( titleKey, {
							'class': [ 'iaew-advanced-tabs__tab-title' ],
							'data-binding-type': 'repeater-item',
							'data-binding-repeater-name': 'tabs',
							'data-binding-setting': [ 'tab_title', 'element_css_id' ],
							'data-binding-index': tabCount,
							'data-binding-config': JSON.stringify( {
								'element_css_id': {
									editType: 'attribute',
									attr: 'id',
									selector: 'button',
								},
								'tab_title': {
									editType: 'text',
								},
							} ),
						}, null, true );

						view.addRenderAttribute( iconKey, {
							'class': [ 'iaew-advanced-tabs__tab-icon' ],
							'data-binding-type': 'repeater-item',
							'data-binding-repeater-name': 'tabs',
							'data-binding-index': tabCount,
						}, null, true );
					#>
						<button {{{ view.getRenderAttributeString( tabKey ) }}}>
							<# if ( tabIcon && tabIcon.value ) { #>
								<span {{{ view.getRenderAttributeString( iconKey ) }}}>{{{ tabIcon.value }}}</span>
							<# } #>
							<span {{{ view.getRenderAttributeString( titleKey ) }}}>{{{ item.tab_title }}}</span>
						</button>
					<# } ); #>
				</div>
				<div class="iaew-advanced-tabs__content"></div>
			<# } #>
		</div>
		<?php
	}

	private function enqueue_widget_assets(): void {
		if ( wp_style_is( 'impact_widgets_elementor_advanced-tabs', 'registered' ) ) {
			wp_enqueue_style( 'impact_widgets_elementor_advanced-tabs' );
		}

		if ( wp_script_is( 'impact_widgets_elementor_advanced-tabs', 'registered' ) ) {
			wp_enqueue_script( 'impact_widgets_elementor_advanced-tabs' );
		}
	}

	private function enqueue_icon_assets(): void {
		if ( isset( Elementor_Plugin::$instance->icons_manager ) && method_exists( Elementor_Plugin::$instance->icons_manager, 'enqueue_fontawesome_css' ) ) {
			Elementor_Plugin::$instance->icons_manager->enqueue_fontawesome_css();
		}

		if ( wp_style_is( 'eicons', 'registered' ) ) {
			wp_enqueue_style( 'eicons' );
		}

		if ( wp_style_is( 'font-awesome-5-all', 'registered' ) ) {
			wp_enqueue_style( 'font-awesome-5-all' );
		}
	}
}
