<?php

namespace ElementPack\Modules\GlobalBadge;

use Elementor\Controls_Manager;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Text_Shadow;
use Elementor\Group_Control_Typography;
use Elementor\Icons_Manager;
use Elementor\Includes\Elements\Container;
use Elementor\Widget_Base;
use ElementPack\Base\Element_Pack_Module_Base;

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

class Module extends Element_Pack_Module_Base {

	/**
	 * Stack of element IDs currently buffering badge output (nested elements).
	 *
	 * @var string[]
	 */
	protected static $buffering_element_ids = [];

	public function __construct() {
		parent::__construct();
		$this->add_actions();
	}

	public function get_name() {
		return 'bdt-global-badge';
	}

	public function register_section( $element ) {
		$pro_lock = bdt_get_widget_badge( str_replace( 'bdt-', '', $this->get_name() ) );
		$tab      = Controls_Manager::TAB_CONTENT;

		if ( in_array( $element->get_name(), [ 'section', 'column', 'container' ], true ) ) {
			$tab = Controls_Manager::TAB_LAYOUT;
		}

		$element->start_controls_section(
			'section_element_pack_global_badge',
			[
				'label' => BDTEP_CP . esc_html__( 'Global Badge', 'bdthemes-element-pack' ) . BDTEP_NC,
				'tab'   => $tab,
			]
		);

		$element->end_controls_section();
	}

	public function register_controls( $widget, $args ) {
		$this->register_general_controls( $widget );
		$this->register_floating_controls( $widget );
		$this->register_style_layout_controls( $widget );
	}

	protected function register_general_controls( $widget ) {
		$widget->add_control(
			'ep_global_badge_enable',
			[
				'label'              => esc_html__( 'Enable Global Badge', 'bdthemes-element-pack' ),
				'type'               => Controls_Manager::SWITCHER,
				'return_value'       => 'yes',
				'frontend_available' => true,
				'prefix_class'       => 'bdt-ep-global-badge-yes',
			]
		);

		$widget->add_control(
			'ep_global_badge_text',
			[
				'label'              => esc_html__( 'Text', 'bdthemes-element-pack' ),
				'type'               => Controls_Manager::TEXT,
				'default'            => esc_html__( 'New', 'bdthemes-element-pack' ),
				'dynamic'            => [ 'active' => true ],
				'frontend_available' => true,
				'render_type'        => 'none',
				'condition'          => [
					'ep_global_badge_enable' => 'yes',
				],
			]
		);

		$widget->add_control(
			'ep_global_badge_style',
			[
				'label'              => esc_html__( 'Style', 'bdthemes-element-pack' ),
				'type'               => Controls_Manager::SELECT,
				'default'            => 'default',
				'options'            => [
					'default'       => esc_html__( 'Default', 'bdthemes-element-pack' ),
					'custom-layout' => esc_html__( 'Custom', 'bdthemes-element-pack' ),
				],
				'frontend_available' => true,
				'render_type'        => 'none',
				'condition'          => [
					'ep_global_badge_enable' => 'yes',
				],
			]
		);

		$widget->add_control(
			'ep_global_badge_icon',
			[
				'label'              => esc_html__( 'Icon', 'bdthemes-element-pack' ),
				'type'               => Controls_Manager::SWITCHER,
				'return_value'       => 'yes',
				'frontend_available' => true,
				'render_type'        => 'none',
				'condition'          => [
					'ep_global_badge_enable' => 'yes',
				],
			]
		);

		$widget->add_control(
			'ep_global_badge_clip_path',
			[
				'label'              => esc_html__( 'Enable Clip Path', 'bdthemes-element-pack' ),
				'type'               => Controls_Manager::SWITCHER,
				'return_value'       => 'yes',
				'frontend_available' => true,
				'render_type'        => 'none',
				'condition'          => [
					'ep_global_badge_enable' => 'yes',
					'ep_global_badge_style'  => 'custom-layout',
				],
			]
		);

		$widget->add_control(
			'ep_global_badge_clip_path_value',
			[
				'label'              => esc_html__( 'Path Value', 'bdthemes-element-pack' ),
				'type'               => Controls_Manager::TEXTAREA,
				'default'            => 'polygon(0% 0%, 100% 1%, 88% 49%, 100% 100%, 0% 100%)',
				'description'        => sprintf(
					/* translators: %s: Clippy link */
					__( 'Get the clip path code from %s.', 'bdthemes-element-pack' ),
					'<a href="https://bennettfeely.com/clippy/" target="_blank" rel="noopener">' . esc_html__( 'Clippy', 'bdthemes-element-pack' ) . '</a>'
				),
				'frontend_available' => true,
				'render_type'        => 'none',
				'condition'          => [
					'ep_global_badge_enable'     => 'yes',
					'ep_global_badge_style'      => 'custom-layout',
					'ep_global_badge_clip_path'  => 'yes',
				],
			]
		);

		$widget->add_control(
			'ep_global_badge_svg_layer',
			[
				'label'              => esc_html__( 'Add SVG Layer', 'bdthemes-element-pack' ),
				'type'               => Controls_Manager::SWITCHER,
				'return_value'       => 'yes',
				'frontend_available' => true,
				'render_type'        => 'none',
				'condition'          => [
					'ep_global_badge_enable' => 'yes',
					'ep_global_badge_style'  => 'custom-layout',
				],
			]
		);

		$widget->add_control(
			'ep_global_badge_svg_code',
			[
				'label'              => esc_html__( 'SVG Code', 'bdthemes-element-pack' ),
				'type'               => Controls_Manager::CODE,
				'language'           => 'xml',
				'frontend_available' => true,
				'description'        => sprintf(
					/* translators: %s: Blobmaker link */
					__( 'Get Blob SVG code from %s', 'bdthemes-element-pack' ),
					'<a href="https://www.blobmaker.app/" target="_blank" rel="noopener"><em>' . esc_html__( 'Blobmaker', 'bdthemes-element-pack' ) . '</em></a>'
				),
				'render_type'        => 'none',
				'condition'          => [
					'ep_global_badge_enable'    => 'yes',
					'ep_global_badge_style'     => 'custom-layout',
					'ep_global_badge_svg_layer' => 'yes',
				],
			]
		);
	}

	protected function register_floating_controls( $widget ) {
		$floating_on = [
			'ep_global_badge_enable'   => 'yes',
			'ep_global_badge_floating' => 'yes',
		];

		$widget->add_control(
			'ep_global_badge_floating',
			[
				'label'              => esc_html__( 'Floating Effects', 'bdthemes-element-pack' ),
				'type'               => Controls_Manager::SWITCHER,
				'return_value'       => 'yes',
				'frontend_available' => true,
				'render_type'        => 'none',
				'separator'          => 'before',
				'condition'          => [
					'ep_global_badge_enable' => 'yes',
				],
			]
		);

		$widget->add_control(
			'ep_global_badge_floating_translate_toggle',
			[
				'label'              => esc_html__( 'Translate', 'bdthemes-element-pack' ),
				'type'               => Controls_Manager::POPOVER_TOGGLE,
				'return_value'       => 'yes',
				'frontend_available' => true,
				'condition'          => $floating_on,
			]
		);

		$widget->start_popover();

		$widget->add_control(
			'ep_global_badge_floating_translate_x',
			[
				'label'              => esc_html__( 'Translate X', 'bdthemes-element-pack' ),
				'type'               => Controls_Manager::SLIDER,
				'default'            => [
					'sizes' => [
						'from' => -5,
						'to'   => 6,
					],
					'unit'  => 'px',
				],
				'range'              => [
					'px' => [
						'min' => -100,
						'max' => 100,
						'step' => 1,
					],
				],
				'labels'             => [
					esc_html__( 'From', 'bdthemes-element-pack' ),
					esc_html__( 'To', 'bdthemes-element-pack' ),
				],
				'scales'             => 1,
				'handles'            => 'range',
				'frontend_available' => true,
				'condition'          => array_merge(
					$floating_on,
					[ 'ep_global_badge_floating_translate_toggle' => 'yes' ]
				),
			]
		);

		$widget->add_control(
			'ep_global_badge_floating_translate_y',
			[
				'label'              => esc_html__( 'Translate Y', 'bdthemes-element-pack' ),
				'type'               => Controls_Manager::SLIDER,
				'default'            => [
					'sizes' => [
						'from' => -5,
						'to'   => 20,
					],
					'unit'  => 'px',
				],
				'range'              => [
					'px' => [
						'min' => -100,
						'max' => 100,
						'step' => 1,
					],
				],
				'labels'             => [
					esc_html__( 'From', 'bdthemes-element-pack' ),
					esc_html__( 'To', 'bdthemes-element-pack' ),
				],
				'scales'             => 1,
				'handles'            => 'range',
				'frontend_available' => true,
				'condition'          => array_merge(
					$floating_on,
					[ 'ep_global_badge_floating_translate_toggle' => 'yes' ]
				),
			]
		);

		$widget->add_control(
			'ep_global_badge_floating_translate_speed',
			[
				'label'              => esc_html__( 'Speed', 'bdthemes-element-pack' ),
				'type'               => Controls_Manager::SLIDER,
				'range'              => [
					'px' => [
						'min'  => 0.1,
						'max'  => 10,
						'step' => 0.1,
					],
				],
				'default'            => [
					'unit' => 'px',
					'size' => 1,
				],
				'frontend_available' => true,
				'condition'          => array_merge(
					$floating_on,
					[ 'ep_global_badge_floating_translate_toggle' => 'yes' ]
				),
			]
		);

		$widget->end_popover();

		$widget->add_control(
			'ep_global_badge_floating_rotate_toggle',
			[
				'label'              => esc_html__( 'Rotate', 'bdthemes-element-pack' ),
				'type'               => Controls_Manager::POPOVER_TOGGLE,
				'return_value'       => 'yes',
				'frontend_available' => true,
				'condition'          => $floating_on,
			]
		);

		$widget->start_popover();

		$rotate_axes = [
			'x' => esc_html__( 'Rotate X', 'bdthemes-element-pack' ),
			'y' => esc_html__( 'Rotate Y', 'bdthemes-element-pack' ),
			'z' => esc_html__( 'Rotate Z', 'bdthemes-element-pack' ),
		];

		foreach ( $rotate_axes as $axis => $rotate_label ) {
			$defaults = [
				'x' => [ 'from' => 0, 'to' => 43 ],
				'y' => [ 'from' => 0, 'to' => 32 ],
				'z' => [ 'from' => -30, 'to' => 45 ],
			];

			$widget->add_control(
				'ep_global_badge_floating_rotate_' . $axis,
				[
					'label'              => $rotate_label,
					'type'               => Controls_Manager::SLIDER,
					'default'            => [
						'sizes' => $defaults[ $axis ],
						'unit'  => 'deg',
					],
					'range'              => [
						'deg' => [
							'min' => -180,
							'max' => 180,
							'step' => 1,
						],
					],
					'labels'             => [
						esc_html__( 'From', 'bdthemes-element-pack' ),
						esc_html__( 'To', 'bdthemes-element-pack' ),
					],
					'scales'             => 1,
					'handles'            => 'range',
					'frontend_available' => true,
					'condition'          => array_merge(
						$floating_on,
						[ 'ep_global_badge_floating_rotate_toggle' => 'yes' ]
					),
				]
			);
		}

		$widget->add_control(
			'ep_global_badge_floating_rotate_speed',
			[
				'label'              => esc_html__( 'Speed', 'bdthemes-element-pack' ),
				'type'               => Controls_Manager::SLIDER,
				'range'              => [
					'px' => [
						'min'  => 0.1,
						'max'  => 10,
						'step' => 0.1,
					],
				],
				'default'            => [
					'unit' => 'px',
					'size' => 3,
				],
				'frontend_available' => true,
				'condition'          => array_merge(
					$floating_on,
					[ 'ep_global_badge_floating_rotate_toggle' => 'yes' ]
				),
			]
		);

		$widget->end_popover();

		$widget->add_control(
			'ep_global_badge_floating_opacity_toggle',
			[
				'label'              => esc_html__( 'Opacity', 'bdthemes-element-pack' ),
				'type'               => Controls_Manager::POPOVER_TOGGLE,
				'return_value'       => 'yes',
				'frontend_available' => true,
				'condition'          => $floating_on,
			]
		);

		$widget->start_popover();

		$widget->add_control(
			'ep_global_badge_floating_opacity_start',
			[
				'label'              => esc_html__( 'Start', 'bdthemes-element-pack' ),
				'type'               => Controls_Manager::SLIDER,
				'range'              => [
					'px' => [
						'min'  => 0,
						'max'  => 1,
						'step' => 0.1,
					],
				],
				'default'            => [
					'unit' => 'px',
					'size' => 1,
				],
				'frontend_available' => true,
				'condition'          => array_merge(
					$floating_on,
					[ 'ep_global_badge_floating_opacity_toggle' => 'yes' ]
				),
			]
		);

		$widget->add_control(
			'ep_global_badge_floating_opacity_end',
			[
				'label'              => esc_html__( 'End', 'bdthemes-element-pack' ),
				'type'               => Controls_Manager::SLIDER,
				'range'              => [
					'px' => [
						'min'  => 0,
						'max'  => 1,
						'step' => 0.1,
					],
				],
				'default'            => [
					'unit' => 'px',
					'size' => 0.5,
				],
				'frontend_available' => true,
				'condition'          => array_merge(
					$floating_on,
					[ 'ep_global_badge_floating_opacity_toggle' => 'yes' ]
				),
			]
		);

		$widget->add_control(
			'ep_global_badge_floating_opacity_speed',
			[
				'label'              => esc_html__( 'Speed', 'bdthemes-element-pack' ),
				'type'               => Controls_Manager::SLIDER,
				'range'              => [
					'px' => [
						'min'  => 0.1,
						'max'  => 10,
						'step' => 0.1,
					],
				],
				'default'            => [
					'unit' => 'px',
					'size' => 1,
				],
				'frontend_available' => true,
				'condition'          => array_merge(
					$floating_on,
					[ 'ep_global_badge_floating_opacity_toggle' => 'yes' ]
				),
			]
		);

		$widget->end_popover();

		$widget->add_control(
			'ep_global_badge_floating_blur_toggle',
			[
				'label'              => esc_html__( 'Blur', 'bdthemes-element-pack' ),
				'type'               => Controls_Manager::POPOVER_TOGGLE,
				'return_value'       => 'yes',
				'frontend_available' => true,
				'condition'          => $floating_on,
			]
		);

		$widget->start_popover();

		$widget->add_control(
			'ep_global_badge_floating_blur_start',
			[
				'label'              => esc_html__( 'Start', 'bdthemes-element-pack' ),
				'type'               => Controls_Manager::SLIDER,
				'range'              => [
					'px' => [
						'min'  => 0,
						'max'  => 20,
						'step' => 1,
					],
				],
				'default'            => [
					'unit' => 'px',
					'size' => 0,
				],
				'frontend_available' => true,
				'condition'          => array_merge(
					$floating_on,
					[ 'ep_global_badge_floating_blur_toggle' => 'yes' ]
				),
			]
		);

		$widget->add_control(
			'ep_global_badge_floating_blur_end',
			[
				'label'              => esc_html__( 'End', 'bdthemes-element-pack' ),
				'type'               => Controls_Manager::SLIDER,
				'range'              => [
					'px' => [
						'min'  => 0,
						'max'  => 20,
						'step' => 1,
					],
				],
				'default'            => [
					'unit' => 'px',
					'size' => 4,
				],
				'frontend_available' => true,
				'condition'          => array_merge(
					$floating_on,
					[ 'ep_global_badge_floating_blur_toggle' => 'yes' ]
				),
			]
		);

		$widget->add_control(
			'ep_global_badge_floating_blur_speed',
			[
				'label'              => esc_html__( 'Speed', 'bdthemes-element-pack' ),
				'type'               => Controls_Manager::SLIDER,
				'range'              => [
					'px' => [
						'min'  => 0.1,
						'max'  => 10,
						'step' => 0.1,
					],
				],
				'default'            => [
					'unit' => 'px',
					'size' => 1,
				],
				'frontend_available' => true,
				'condition'          => array_merge(
					$floating_on,
					[ 'ep_global_badge_floating_blur_toggle' => 'yes' ]
				),
			]
		);

		$widget->end_popover();
	}

	protected function register_style_layout_controls( $widget ) {
		$widget->add_control(
			'ep_global_badge_style_layout_heading',
			[
				'label'     => esc_html__( 'Style & Layout', 'bdthemes-element-pack' ),
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
				'condition' => [
					'ep_global_badge_enable' => 'yes',
				],
			]
		);

		$widget->start_controls_tabs(
			'ep_global_badge_style_layout_tabs',
			[
				'condition' => [
					'ep_global_badge_enable' => 'yes',
				],
			]
		);

		$this->register_layout_tab_controls( $widget );
		$this->register_style_tab_controls( $widget );
		$this->register_icon_tab_controls( $widget );
		$this->register_svg_layer_tab_controls( $widget );

		$widget->end_controls_tabs();
	}

	protected function register_layout_tab_controls( $widget ) {
		$widget->start_controls_tab(
			'ep_global_badge_tab_layout',
			[
				'label' => esc_html__( 'Layout', 'bdthemes-element-pack' ),
			]
		);

		$widget->add_control(
			'ep_global_badge_horizontal',
			[
				'label'              => esc_html__( 'Horizontal Position', 'bdthemes-element-pack' ),
				'type'               => Controls_Manager::CHOOSE,
				'default'            => 'right',
				'options'            => [
					'left'  => [
						'title' => esc_html__( 'Left', 'bdthemes-element-pack' ),
						'icon'  => 'eicon-h-align-left',
					],
					'right' => [
						'title' => esc_html__( 'Right', 'bdthemes-element-pack' ),
						'icon'  => 'eicon-h-align-right',
					],
				],
				'toggle'             => false,
				'frontend_available' => true,
				'render_type'        => 'none',
			]
		);

		$widget->add_control(
			'ep_global_badge_vertical',
			[
				'label'              => esc_html__( 'Vertical Position', 'bdthemes-element-pack' ),
				'type'               => Controls_Manager::CHOOSE,
				'default'            => 'top',
				'options'            => [
					'top'    => [
						'title' => esc_html__( 'Top', 'bdthemes-element-pack' ),
						'icon'  => 'eicon-v-align-top',
					],
					'bottom' => [
						'title' => esc_html__( 'Bottom', 'bdthemes-element-pack' ),
						'icon'  => 'eicon-v-align-bottom',
					],
				],
				'toggle'             => false,
				'frontend_available' => true,
				'render_type'        => 'none',
			]
		);

		$widget->add_responsive_control(
			'ep_global_badge_offset_x',
			[
				'label'      => esc_html__( 'Horizontal Offset', 'bdthemes-element-pack' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => [ 'px', '%', 'em' ],
				'range'      => [
					'px' => [
						'min' => -200,
						'max' => 200,
					],
				],
				'default'    => [
					'unit' => 'px',
					'size' => 0,
				],
				'frontend_available' => true,
				'selectors'          => [
					'{{WRAPPER}} .bdt-ep-global-badge' => '--ep-global-badge-offset-x: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$widget->add_responsive_control(
			'ep_global_badge_offset_y',
			[
				'label'      => esc_html__( 'Vertical Offset', 'bdthemes-element-pack' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => [ 'px', '%', 'em' ],
				'range'      => [
					'px' => [
						'min' => -200,
						'max' => 200,
					],
				],
				'default'    => [
					'unit' => 'px',
					'size' => 0,
				],
				'frontend_available' => true,
				'selectors'          => [
					'{{WRAPPER}} .bdt-ep-global-badge' => '--ep-global-badge-offset-y: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$widget->add_responsive_control(
			'ep_global_badge_rotate',
			[
				'label'      => esc_html__( 'Rotate (Degrees)', 'bdthemes-element-pack' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => [ 'deg' ],
				'range'      => [
					'deg' => [
						'min' => -180,
						'max' => 180,
					],
				],
				'default'    => [
					'unit' => 'deg',
					'size' => 15,
				],
				'frontend_available' => true,
				'selectors'          => [
					'{{WRAPPER}} .bdt-ep-global-badge' => '--ep-global-badge-rotate: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$widget->add_control(
			'ep_global_badge_overflow_hidden',
			[
				'label'              => esc_html__( 'Overflow Hidden', 'bdthemes-element-pack' ),
				'type'               => Controls_Manager::SWITCHER,
				'return_value'       => 'yes',
				'frontend_available' => true,
				'prefix_class'       => 'bdt-ep-global-badge-overflow-hidden-',
				'condition'          => [
					'ep_global_badge_enable' => 'yes',
				],
			]
		);

		$widget->end_controls_tab();
	}

	protected function register_style_tab_controls( $widget ) {
		$widget->start_controls_tab(
			'ep_global_badge_tab_style',
			[
				'label' => esc_html__( 'Style', 'bdthemes-element-pack' ),
			]
		);

		$widget->add_responsive_control(
			'ep_global_badge_size',
			[
				'label'      => esc_html__( 'Size', 'bdthemes-element-pack' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => [ 'px' ],
				'range'      => [
					'px' => [
						'min' => 20,
						'max' => 400,
					],
				],
				'default'    => [
					'unit' => 'px',
					'size' => 100,
				],
				'selectors'  => [
					'{{WRAPPER}} .bdt-ep-global-badge-inner' => 'min-inline-size: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$widget->add_control(
			'ep_global_badge_text_color',
			[
				'label'     => esc_html__( 'Text Color', 'bdthemes-element-pack' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .bdt-ep-global-badge-text' => 'color: {{VALUE}};',
				],
			]
		);

		$widget->add_group_control(
			Group_Control_Background::get_type(),
			[
				'name'     => 'ep_global_badge_background',
				'selector' => '{{WRAPPER}} .bdt-ep-global-badge-inner',
			]
		);

		$widget->add_group_control(
			Group_Control_Border::get_type(),
			[
				'name'     => 'ep_global_badge_border',
				'selector' => '{{WRAPPER}} .bdt-ep-global-badge-inner',
			]
		);

		$widget->add_responsive_control(
			'ep_global_badge_border_radius',
			[
				'label'      => esc_html__( 'Border Radius', 'bdthemes-element-pack' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%' ],
				'selectors'  => [
					'{{WRAPPER}} .bdt-ep-global-badge-inner' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$widget->add_responsive_control(
			'ep_global_badge_padding',
			[
				'label'      => esc_html__( 'Padding', 'bdthemes-element-pack' ),
				'type'       => Controls_Manager::DIMENSIONS,
				'size_units' => [ 'px', '%', 'em' ],
				'selectors'  => [
					'{{WRAPPER}} .bdt-ep-global-badge-inner' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
				],
			]
		);

		$widget->add_group_control(
			Group_Control_Typography::get_type(),
			[
				'name'     => 'ep_global_badge_typography',
				'selector' => '{{WRAPPER}} .bdt-ep-global-badge-text',
			]
		);

		$widget->add_group_control(
			Group_Control_Text_Shadow::get_type(),
			[
				'name'     => 'ep_global_badge_text_shadow',
				'selector' => '{{WRAPPER}} .bdt-ep-global-badge-text',
			]
		);

		$widget->add_group_control(
			Group_Control_Box_Shadow::get_type(),
			[
				'name'     => 'ep_global_badge_box_shadow',
				'selector' => '{{WRAPPER}} .bdt-ep-global-badge-inner',
			]
		);

		$widget->end_controls_tab();
	}

	protected function register_icon_tab_controls( $widget ) {
		$widget->start_controls_tab(
			'ep_global_badge_tab_icon',
			[
				'label'     => esc_html__( 'Icon', 'bdthemes-element-pack' ),
				'condition' => [
					'ep_global_badge_icon' => 'yes',
				],
			]
		);

		$widget->add_control(
			'ep_global_badge_icon_type',
			[
				'label'              => esc_html__( 'Icon Type', 'bdthemes-element-pack' ),
				'type'               => Controls_Manager::SELECT,
				'default'            => 'icon',
				'options'            => [
					'icon'   => esc_html__( 'Icon', 'bdthemes-element-pack' ),
					'lottie' => esc_html__( 'Lottie', 'bdthemes-element-pack' ),
				],
				'frontend_available' => true,
				'render_type'        => 'template',
			]
		);

		$widget->add_control(
			'ep_global_badge_icon_selected',
			[
				'label'   => esc_html__( 'Icon', 'bdthemes-element-pack' ),
				'type'    => Controls_Manager::ICONS,
				'default' => [
					'value'   => 'fas fa-star',
					'library' => 'fa-solid',
				],
				'render_type'        => 'template',
				'frontend_available' => true,
				'condition'          => [
					'ep_global_badge_icon_type' => 'icon',
				],
			]
		);

		$widget->add_control(
			'ep_global_badge_lottie_json_source',
			[
				'label'              => esc_html__( 'Select JSON Source', 'bdthemes-element-pack' ),
				'type'               => Controls_Manager::SELECT,
				'default'            => 'url',
				'options'            => [
					'url'    => esc_html__( 'Load From URL', 'bdthemes-element-pack' ),
					'local'  => esc_html__( 'Self Hosted', 'bdthemes-element-pack' ),
					'custom' => esc_html__( 'Custom JSON Code', 'bdthemes-element-pack' ),
				],
				'frontend_available' => true,
				'render_type'        => 'template',
				'condition'          => [
					'ep_global_badge_icon_type' => 'lottie',
				],
			]
		);

		$widget->add_control(
			'ep_global_badge_lottie_json_path',
			[
				'label'              => esc_html__( 'Lottie JSON URL', 'bdthemes-element-pack' ),
				'description'        => sprintf(
					/* translators: %1$s: opening anchor, %2$s: closing anchor */
					esc_html__( 'Enter your Lottie JSON file URL. Get free animations from %1$sLottieFiles%2$s.', 'bdthemes-element-pack' ),
					'<a href="https://lottiefiles.com/featured" target="_blank" rel="noopener">',
					'</a>'
				),
				'type'               => Controls_Manager::TEXT,
				'autocomplete'       => false,
				'show_external'      => false,
				'label_block'        => true,
				'default'            => BDTEP_ASSETS_URL . 'others/rocket-space.json',
				'placeholder'        => esc_html__( 'Enter your JSON URL', 'bdthemes-element-pack' ),
				'dynamic'            => [
					'active' => true,
				],
				'frontend_available' => true,
				'render_type'        => 'template',
				'condition'          => [
					'ep_global_badge_icon_type'        => 'lottie',
					'ep_global_badge_lottie_json_source' => 'url',
				],
			]
		);

		$widget->add_control(
			'ep_global_badge_lottie_upload_json_file',
			[
				'label'              => esc_html__( 'Select JSON File', 'bdthemes-element-pack' ),
				'type'               => 'json-upload',
				'label_block'        => true,
				'dynamic'            => [
					'active' => true,
				],
				'frontend_available' => true,
				'render_type'        => 'template',
				'condition'          => [
					'ep_global_badge_icon_type'        => 'lottie',
					'ep_global_badge_lottie_json_source' => 'local',
				],
			]
		);

		$widget->add_control(
			'ep_global_badge_lottie_json_code',
			[
				'label'              => esc_html__( 'Paste JSON Code', 'bdthemes-element-pack' ),
				'description'        => sprintf(
					/* translators: %1$s: opening anchor, %2$s: closing anchor */
					esc_html__( 'Paste your Lottie JSON. Get free animations from %1$sLottieFiles%2$s.', 'bdthemes-element-pack' ),
					'<a href="https://lottiefiles.com/featured" target="_blank" rel="noopener">',
					'</a>'
				),
				'type'               => Controls_Manager::TEXTAREA,
				'label_block'        => true,
				'placeholder'        => esc_html__( 'Enter your JSON text', 'bdthemes-element-pack' ),
				'dynamic'            => [
					'active' => true,
				],
				'frontend_available' => true,
				'render_type'        => 'template',
				'condition'          => [
					'ep_global_badge_icon_type'        => 'lottie',
					'ep_global_badge_lottie_json_source' => 'custom',
				],
			]
		);

		$widget->add_control(
			'ep_global_badge_lottie_loop',
			[
				'label'              => esc_html__( 'Loop', 'bdthemes-element-pack' ),
				'type'               => Controls_Manager::SWITCHER,
				'default'            => 'yes',
				'frontend_available' => true,
				'render_type'        => 'template',
				'condition'          => [
					'ep_global_badge_icon_type' => 'lottie',
				],
			]
		);

		$widget->add_control(
			'ep_global_badge_lottie_speed',
			[
				'label'              => esc_html__( 'Play Speed', 'bdthemes-element-pack' ),
				'type'               => Controls_Manager::SLIDER,
				'range'              => [
					'px' => [
						'min'  => 0.1,
						'max'  => 3,
						'step' => 0.1,
					],
				],
				'default'            => [
					'size' => 1,
				],
				'frontend_available' => true,
				'render_type'        => 'template',
				'condition'          => [
					'ep_global_badge_icon_type' => 'lottie',
				],
			]
		);

		$widget->add_control(
			'ep_global_badge_lottie_start_point',
			[
				'label'              => esc_html__( 'Start Point', 'bdthemes-element-pack' ),
				'type'               => Controls_Manager::SLIDER,
				'default'            => [
					'size' => 0,
					'unit' => '%',
				],
				'size_units'         => [ '%' ],
				'frontend_available' => true,
				'render_type'        => 'template',
				'condition'          => [
					'ep_global_badge_icon_type' => 'lottie',
				],
			]
		);

		$widget->add_control(
			'ep_global_badge_lottie_end_point',
			[
				'label'              => esc_html__( 'End Point', 'bdthemes-element-pack' ),
				'type'               => Controls_Manager::SLIDER,
				'default'            => [
					'size' => 100,
					'unit' => '%',
				],
				'size_units'         => [ '%' ],
				'frontend_available' => true,
				'render_type'        => 'template',
				'condition'          => [
					'ep_global_badge_icon_type' => 'lottie',
				],
			]
		);

		$widget->add_control(
			'ep_global_badge_lottie_renderer',
			[
				'label'              => esc_html__( 'Renderer', 'bdthemes-element-pack' ),
				'type'               => Controls_Manager::SELECT,
				'default'            => 'svg',
				'options'            => [
					'svg'    => esc_html__( 'SVG', 'bdthemes-element-pack' ),
					'canvas' => esc_html__( 'Canvas', 'bdthemes-element-pack' ),
				],
				'frontend_available' => true,
				'render_type'        => 'template',
				'condition'          => [
					'ep_global_badge_icon_type' => 'lottie',
				],
			]
		);

		$widget->add_control(
			'ep_global_badge_icon_position',
			[
				'label'                => esc_html__( 'Icon Position', 'bdthemes-element-pack' ),
				'type'                 => Controls_Manager::CHOOSE,
				'default'              => 'left',
				'options'              => [
					'left'  => [
						'title' => esc_html__( 'Left', 'bdthemes-element-pack' ),
						'icon'  => 'eicon-h-align-left',
					],
					'right' => [
						'title' => esc_html__( 'Right', 'bdthemes-element-pack' ),
						'icon'  => 'eicon-h-align-right',
					],
				],
				'toggle'               => false,
				'selectors'            => [
					'{{WRAPPER}} .bdt-ep-global-badge-content' => 'flex-direction: {{VALUE}};',
				],
				'selectors_dictionary' => [
					'left'  => 'row',
					'right' => 'row-reverse',
				],
			]
		);

		$widget->add_responsive_control(
			'ep_global_badge_icon_size',
			[
				'label'      => esc_html__( 'Size', 'bdthemes-element-pack' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => [ 'px', 'em' ],
				'range'      => [
					'px' => [
						'min' => 6,
						'max' => 100,
					],
				],
				'default'    => [
					'unit' => 'px',
					'size' => 12,
				],
				'selectors'  => [
					'{{WRAPPER}} .bdt-ep-global-badge-icon' => 'font-size: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .bdt-ep-global-badge-icon svg' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .bdt-ep-global-badge-icon .bdt-lottie-container' => 'inline-size: {{SIZE}}{{UNIT}}; block-size: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$widget->add_control(
			'ep_global_badge_icon_color',
			[
				'label'     => esc_html__( 'Color', 'bdthemes-element-pack' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .bdt-ep-global-badge-icon' => 'color: {{VALUE}};',
					'{{WRAPPER}} .bdt-ep-global-badge-icon svg' => 'fill: {{VALUE}};',
				],
				'condition' => [
					'ep_global_badge_icon_type' => 'icon',
				],
			]
		);

		$widget->add_control(
			'ep_global_badge_lottie_icon_fill_color',
			[
				'label'     => esc_html__( 'Fill Color', 'bdthemes-element-pack' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .bdt-ep-global-badge-icon .bdt-lottie-container svg *' => 'fill: {{VALUE}};',
				],
				'condition' => [
					'ep_global_badge_icon_type' => 'lottie',
				],
			]
		);

		$widget->add_control(
			'ep_global_badge_lottie_icon_stroke_color',
			[
				'label'     => esc_html__( 'Stroke Color', 'bdthemes-element-pack' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .bdt-ep-global-badge-icon .bdt-lottie-container svg *' => 'stroke: {{VALUE}};',
				],
				'condition' => [
					'ep_global_badge_icon_type' => 'lottie',
				],
			]
		);

		$widget->add_responsive_control(
			'ep_global_badge_icon_gap',
			[
				'label'      => esc_html__( 'Gap', 'bdthemes-element-pack' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => [ 'px' ],
				'range'      => [
					'px' => [
						'min' => 0,
						'max' => 50,
					],
				],
				'default'    => [
					'unit' => 'px',
					'size' => 6,
				],
				'selectors'  => [
					'{{WRAPPER}} .bdt-ep-global-badge-content' => 'gap: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$widget->end_controls_tab();
	}

	protected function register_svg_layer_tab_controls( $widget ) {
		$widget->start_controls_tab(
			'ep_global_badge_tab_svg',
			[
				'label'     => esc_html__( 'SVG Layer', 'bdthemes-element-pack' ),
				'condition' => [
					'ep_global_badge_svg_layer' => 'yes',
					'ep_global_badge_style'     => 'custom-layout',
				],
			]
		);

		$widget->add_responsive_control(
			'ep_global_badge_svg_width',
			[
				'label'      => esc_html__( 'Width', 'bdthemes-element-pack' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => [ 'px', '%' ],
				'range'      => [
					'px' => [
						'min' => 10,
						'max' => 500,
					],
				],
				'default'    => [
					'unit' => 'px',
					'size' => 100,
				],
				'selectors'  => [
					'{{WRAPPER}} .bdt-ep-global-badge-svg' => 'inline-size: {{SIZE}}{{UNIT}}; block-size: {{SIZE}}{{UNIT}};',
					'{{WRAPPER}} .bdt-ep-global-badge-inner' => 'min-inline-size: {{SIZE}}{{UNIT}}; min-block-size: {{SIZE}}{{UNIT}};',
				],
				'condition'  => [
					'ep_global_badge_svg_layer' => 'yes',
				],
			]
		);

		$widget->add_control(
			'ep_global_badge_svg_color',
			[
				'label'     => esc_html__( 'Color', 'bdthemes-element-pack' ),
				'type'      => Controls_Manager::COLOR,
				'selectors' => [
					'{{WRAPPER}} .bdt-ep-global-badge-svg svg *' => 'fill: {{VALUE}};',
				],
			]
		);

		$widget->add_responsive_control(
			'ep_global_badge_svg_offset_x',
			[
				'label'      => esc_html__( 'Horizontal Offset', 'bdthemes-element-pack' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => [ 'px', '%' ],
				'range'      => [
					'px' => [
						'min' => -200,
						'max' => 200,
					],
				],
				'selectors'  => [
					'{{WRAPPER}} .bdt-ep-global-badge-svg' => 'inset-inline-start: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$widget->add_responsive_control(
			'ep_global_badge_svg_offset_y',
			[
				'label'      => esc_html__( 'Vertical Offset', 'bdthemes-element-pack' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => [ 'px', '%' ],
				'range'      => [
					'px' => [
						'min' => -200,
						'max' => 200,
					],
				],
				'selectors'  => [
					'{{WRAPPER}} .bdt-ep-global-badge-svg' => 'inset-block-start: {{SIZE}}{{UNIT}};',
				],
			]
		);

		$widget->end_controls_tab();
	}

	public function before_render( $element ) {
		if ( ! $element instanceof \Elementor\Element_Base ) {
			return;
		}

		if ( 'yes' !== $element->get_settings_for_display( 'ep_global_badge_enable' ) ) {
			return;
		}

		$element->add_render_attribute( '_wrapper', 'class', 'bdt-ep-global-badge-active' );
	}

	/**
	 * @param array  $settings     Element settings.
	 * @param string $key          Setting key.
	 * @param string $default_unit Default CSS unit.
	 */
	protected function get_slider_dimension( $settings, $key, $default_unit = 'px' ) {
		if ( empty( $settings[ $key ] ) || ! isset( $settings[ $key ]['size'] ) || '' === $settings[ $key ]['size'] ) {
			return null;
		}

		$unit = ! empty( $settings[ $key ]['unit'] ) ? $settings[ $key ]['unit'] : $default_unit;

		return $settings[ $key ]['size'] . $unit;
	}

	/**
	 * @param array $settings Element settings.
	 */
	protected function get_badge_layout_transform_style( $settings ) {
		$offset_x = $this->get_slider_dimension( $settings, 'ep_global_badge_offset_x' ) ?? '0px';
		$offset_y = $this->get_slider_dimension( $settings, 'ep_global_badge_offset_y' ) ?? '0px';
		$rotate   = $this->get_slider_dimension( $settings, 'ep_global_badge_rotate', 'deg' ) ?? '0deg';

		return sprintf( 'transform: translate(%1$s, %2$s) rotate(%3$s);', $offset_x, $offset_y, $rotate );
	}

	/**
	 * Returns combined UIkit corner class e.g. top-right (not top + right separately).
	 */
	protected function get_badge_position_class( $horizontal, $vertical ) {
		$positions = [
			'left'  => [
				'top'    => 'top-left',
				'bottom' => 'bottom-left',
			],
			'right' => [
				'top'    => 'top-right',
				'bottom' => 'bottom-right',
			],
		];

		return $positions[ $horizontal ][ $vertical ] ?? 'top-right';
	}

	/**
	 * @param array $settings Element settings.
	 */
	protected function get_badge_style_slug( $settings ) {
		$style = $settings['ep_global_badge_style'] ?? 'default';

		if ( 'custom-layout' === $style ) {
			return 'custom-layout';
		}

		return 'default';
	}

	public function render_badge_markup( $element ) {
		$settings = $element->get_settings_for_display();
		$style    = $this->get_badge_style_slug( $settings );
		$h_pos    = ! empty( $settings['ep_global_badge_horizontal'] ) ? $settings['ep_global_badge_horizontal'] : 'right';
		$v_pos    = ! empty( $settings['ep_global_badge_vertical'] ) ? $settings['ep_global_badge_vertical'] : 'top';

		$has_svg = 'yes' === ( $settings['ep_global_badge_svg_layer'] ?? '' ) && ! empty( $settings['ep_global_badge_svg_code'] );

		$classes = [
			'bdt-ep-global-badge',
			'bdt-position-small',
			'bdt-position-' . $this->get_badge_position_class( $h_pos, $v_pos ),
			'bdt-ep-global-badge--' . $style,
		];

		if ( $has_svg ) {
			$classes[] = 'bdt-ep-global-badge-has-svg';
		}

		$badge_style = $this->get_badge_layout_transform_style( $settings );
		$inner_style = $this->get_badge_svg_inner_style( $settings );

		if ( 'yes' === ( $settings['ep_global_badge_clip_path'] ?? '' ) && ! empty( $settings['ep_global_badge_clip_path_value'] ) ) {
			$clip_path    = trim( $settings['ep_global_badge_clip_path_value'] );
			$inner_style .= 'clip-path:' . esc_attr( $clip_path ) . ';-webkit-clip-path:' . esc_attr( $clip_path ) . ';';
		}

		?>
		<div class="<?php echo esc_attr( implode( ' ', $classes ) ); ?>" data-ep-global-badge-for="<?php echo esc_attr( $element->get_id() ); ?>" style="<?php echo esc_attr( $badge_style ); ?>">
			<div class="bdt-ep-global-badge-inner"<?php echo $inner_style ? ' style="' . esc_attr( $inner_style ) . '"' : ''; ?>>
				<?php if ( $has_svg ) : ?>
					<div class="bdt-ep-global-badge-svg" aria-hidden="true">
						<?php echo $this->sanitize_badge_svg_code( $settings['ep_global_badge_svg_code'] ); ?>
					</div>
				<?php endif; ?>
				<div class="bdt-ep-global-badge-content">
					<?php $this->render_badge_icon_markup( $element, $settings ); ?>
					<?php if ( ! empty( $settings['ep_global_badge_text'] ) ) : ?>
						<span class="bdt-ep-global-badge-text"><?php echo esc_html( $settings['ep_global_badge_text'] ); ?></span>
					<?php endif; ?>
				</div>
			</div>
		</div>
		<?php
	}

	public function register_assets() {
		$style_deps  = $this->get_style_dependencies();
		$script_deps = $this->get_script_dependencies();

		if ( wp_style_is( 'ep-global-badge', 'registered' ) ) {
			wp_deregister_style( 'ep-global-badge' );
		}

		wp_register_style(
			'ep-global-badge',
			BDTEP_ASSETS_URL . 'css/ep-global-badge.css',
			$style_deps,
			BDTEP_VER
		);

		if ( ! wp_script_is( 'anime', 'registered' ) ) {
			wp_register_script( 'anime', BDTEP_ASSETS_URL . 'vendor/js/anime.min.js', [], '3.2.1', true );
		}

		if ( wp_script_is( 'ep-global-badge', 'registered' ) ) {
			wp_deregister_script( 'ep-global-badge' );
		}

		wp_register_script(
			'ep-global-badge',
			BDTEP_ASSETS_URL . 'js/modules/ep-global-badge.min.js',
			$script_deps,
			BDTEP_VER,
			true
		);
	}

	/**
	 * @return string[]
	 */
	protected function get_style_dependencies() {
		$deps = [];

		if ( wp_style_is( 'bdt-uikit', 'registered' ) || wp_style_is( 'bdt-uikit', 'enqueued' ) ) {
			$deps[] = 'bdt-uikit';
		}

		return $deps;
	}

	/**
	 * Match Element Pack extension scripts (jquery + bdt-uikit). Handler runs on elementor/frontend/init.
	 *
	 * @return string[]
	 */
	protected function get_script_dependencies() {
		$deps = [ 'jquery' ];

		if ( wp_script_is( 'bdt-uikit', 'registered' ) || wp_script_is( 'bdt-uikit', 'enqueued' ) ) {
			$deps[] = 'bdt-uikit';
		}

		return $deps;
	}

	/**
	 * @param array $settings Element settings.
	 */
	protected function get_badge_icon_type( $settings ) {
		$type = $settings['ep_global_badge_icon_type'] ?? 'icon';

		return 'lottie' === $type ? 'lottie' : 'icon';
	}

	/**
	 * @param mixed $upload_file Uploaded JSON value from json-upload control.
	 */
	protected function resolve_badge_lottie_json_path( $upload_file ) {
		if ( is_array( $upload_file ) ) {
			if ( ! empty( $upload_file['url'] ) ) {
				return $upload_file['url'];
			}

			if ( ! empty( $upload_file['id'] ) ) {
				$url = wp_get_attachment_url( (int) $upload_file['id'] );

				if ( $url ) {
					return $url;
				}
			}
		}

		if ( is_numeric( $upload_file ) ) {
			$url = wp_get_attachment_url( (int) $upload_file );

			if ( $url ) {
				return $url;
			}
		}

		return is_string( $upload_file ) ? $upload_file : '';
	}

	/**
	 * @param array $settings Element settings.
	 */
	protected function has_badge_lottie_content( $settings ) {
		$source = $settings['ep_global_badge_lottie_json_source'] ?? 'url';

		if ( 'url' === $source ) {
			return ! empty( $settings['ep_global_badge_lottie_json_path'] );
		}

		if ( 'local' === $source ) {
			return ! empty( $this->resolve_badge_lottie_json_path( $settings['ep_global_badge_lottie_upload_json_file'] ?? '' ) );
		}

		if ( 'custom' === $source ) {
			return ! empty( trim( $settings['ep_global_badge_lottie_json_code'] ?? '' ) );
		}

		return false;
	}

	/**
	 * @param array $settings Element settings.
	 */
	protected function has_badge_icon_content( $settings ) {
		if ( 'yes' !== ( $settings['ep_global_badge_icon'] ?? '' ) ) {
			return false;
		}

		if ( 'lottie' === $this->get_badge_icon_type( $settings ) ) {
			return $this->has_badge_lottie_content( $settings );
		}

		return ! empty( $settings['ep_global_badge_icon_selected']['value'] );
	}

	/**
	 * @param \Elementor\Element_Base $element  Host element.
	 * @param array                   $settings Element settings.
	 */
	public function element_uses_lottie_icon( $element ) {
		if ( ! $this->is_badge_enabled( $element ) ) {
			return false;
		}

		$settings = $element->get_settings_for_display();

		return 'lottie' === $this->get_badge_icon_type( $settings ) && $this->has_badge_lottie_content( $settings );
	}

	/**
	 * @param array $settings Element settings.
	 * @return array<string, mixed>
	 */
	protected function get_badge_lottie_data_settings( $settings ) {
		$json_code   = '';
		$json_path   = '';
		$is_json_url = true;
		$source      = $settings['ep_global_badge_lottie_json_source'] ?? 'url';

		if ( 'url' === $source ) {
			$json_path = $settings['ep_global_badge_lottie_json_path'] ?? '';
		} elseif ( 'local' === $source ) {
			$json_path = $this->resolve_badge_lottie_json_path( $settings['ep_global_badge_lottie_upload_json_file'] ?? '' );
		} elseif ( 'custom' === $source ) {
			$json_code   = $settings['ep_global_badge_lottie_json_code'] ?? '';
			$is_json_url = false;
		}

		$start_point = ! empty( $settings['ep_global_badge_lottie_start_point']['size'] )
			? $settings['ep_global_badge_lottie_start_point']['size']
			: 0;
		$end_point   = isset( $settings['ep_global_badge_lottie_end_point']['size'] )
			? $settings['ep_global_badge_lottie_end_point']['size']
			: 100;

		if ( '' === $end_point || null === $end_point ) {
			$end_point = 100;
		}

		$loop = 'yes' === ( $settings['ep_global_badge_lottie_loop'] ?? 'yes' );

		return [
			'loop'            => $loop,
			'is_json_url'     => $is_json_url,
			'json_path'       => $json_path,
			'json_code'       => $json_code,
			'speed'           => ! empty( $settings['ep_global_badge_lottie_speed']['size'] ) ? $settings['ep_global_badge_lottie_speed']['size'] : 1,
			'play_action'     => 'autoplay',
			'start_point'     => $start_point,
			'end_point'       => $end_point,
			'lottie_renderer' => ! empty( $settings['ep_global_badge_lottie_renderer'] ) ? $settings['ep_global_badge_lottie_renderer'] : 'svg',
		];
	}

	/**
	 * @param \Elementor\Element_Base $element  Host element.
	 * @param array                   $settings Element settings.
	 */
	protected function render_badge_lottie_icon( $element, $settings ) {
		if ( ! $this->has_badge_lottie_content( $settings ) ) {
			return;
		}

		$lottie_id = 'bdt-ep-global-badge-lottie-' . $element->get_id();
		?>
		<div
			id="<?php echo esc_attr( $lottie_id ); ?>"
			class="bdt-lottie-container"
			data-settings="<?php echo esc_attr( wp_json_encode( $this->get_badge_lottie_data_settings( $settings ) ) ); ?>"
			aria-hidden="true"
		></div>
		<?php
	}

	/**
	 * @param \Elementor\Element_Base $element  Host element.
	 * @param array                   $settings Element settings.
	 */
	protected function render_badge_icon_markup( $element, $settings ) {
		if ( ! $this->has_badge_icon_content( $settings ) ) {
			return;
		}

		$icon_type = $this->get_badge_icon_type( $settings );
		?>
		<span class="bdt-ep-global-badge-icon bdt-ep-global-badge-icon--<?php echo esc_attr( $icon_type ); ?>">
			<?php
			if ( 'lottie' === $icon_type ) {
				$this->render_badge_lottie_icon( $element, $settings );
			} else {
				Icons_Manager::render_icon( $settings['ep_global_badge_icon_selected'], [ 'aria-hidden' => 'true' ] );
			}
			?>
		</span>
		<?php
	}

	/**
	 * Enqueue badge assets (notation-style: used on preview + per-element frontend).
	 */
	public function enqueue_scripts( $element = null ) {
		$this->register_assets();

		wp_enqueue_style( 'ep-global-badge' );
		wp_enqueue_script( 'ep-global-badge' );

		// Preview iframe (editor canvas) or frontend elements using a Lottie badge icon.
		if ( ! $element instanceof \Elementor\Element_Base ) {
			wp_enqueue_script( 'lottie' );
		} elseif ( $this->element_uses_lottie_icon( $element ) ) {
			wp_enqueue_script( 'lottie' );
		}
	}

	public function should_script_enqueue( $element ) {
		if ( ! $element instanceof \Elementor\Element_Base ) {
			return;
		}

		if ( ! $this->is_badge_enabled( $element ) ) {
			return;
		}

		$this->enqueue_scripts( $element );

		if ( 'yes' === $element->get_settings_for_display( 'ep_global_badge_floating' ) ) {
			wp_enqueue_script( 'anime' );
		}
	}

	public function is_badge_enabled( $element ) {
		if ( ! $element instanceof \Elementor\Element_Base ) {
			return false;
		}

		return 'yes' === $element->get_settings_for_display( 'ep_global_badge_enable' );
	}

	/**
	 * PHP renders badge on frontend and Elementor preview. Editor canvas uses JS only.
	 */
	protected function should_inject_badge_via_php() {
		if ( ! class_exists( '\Elementor\Plugin' ) ) {
			return true;
		}

		$elementor = \Elementor\Plugin::$instance;

		if ( $elementor->editor && $elementor->editor->is_edit_mode() ) {
			return false;
		}

		return true;
	}

	/**
	 * @param array $settings Element settings.
	 */
	protected function get_badge_svg_inner_style( $settings ) {
		if ( 'yes' !== ( $settings['ep_global_badge_svg_layer'] ?? '' ) || empty( $settings['ep_global_badge_svg_code'] ) ) {
			return '';
		}

		$width = $settings['ep_global_badge_svg_width'] ?? [];
		$size  = ! empty( $width['size'] ) ? $width['size'] : 100;
		$unit  = ! empty( $width['unit'] ) ? $width['unit'] : 'px';

		return sprintf(
			'overflow:visible;min-inline-size:%1$s%2$s;min-block-size:%1$s%2$s;',
			esc_attr( $size ),
			esc_attr( $unit )
		);
	}

	/**
	 * @param string $svg_code Raw SVG markup.
	 */
	protected function sanitize_badge_svg_code( $svg_code ) {
		$allowed = element_pack_allow_tags( 'svg' );

		$attr_keys = [
			'fill',
			'stroke',
			'stroke-width',
			'opacity',
			'fill-opacity',
			'stroke-opacity',
			'transform',
			'style',
			'class',
		];

		foreach ( [ 'path', 'g', 'circle', 'ellipse', 'rect', 'line' ] as $tag ) {
			if ( ! isset( $allowed[ $tag ] ) || ! is_array( $allowed[ $tag ] ) ) {
				$allowed[ $tag ] = [];
			}

			foreach ( $attr_keys as $attr ) {
				$allowed[ $tag ][ $attr ] = true;
			}
		}

		if ( isset( $allowed['svg'] ) && is_array( $allowed['svg'] ) ) {
			foreach ( array_merge( $attr_keys, [ 'width', 'height', 'viewbox', 'xmlns', 'xmlns:xlink', 'preserveaspectratio' ] ) as $attr ) {
				$allowed['svg'][ $attr ] = true;
			}
		}

		$allowed['defs']            = [ 'class' => true ];
		$allowed['lineargradient']  = [
			'id'              => true,
			'x1'              => true,
			'y1'              => true,
			'x2'              => true,
			'y2'              => true,
			'gradientunits'   => true,
			'gradienttransform' => true,
		];
		$allowed['radialgradient']  = [
			'id'            => true,
			'cx'            => true,
			'cy'            => true,
			'r'             => true,
			'fx'            => true,
			'fy'            => true,
			'gradientunits' => true,
		];
		$allowed['stop']            = [
			'offset'       => true,
			'stop-color'   => true,
			'stop-opacity' => true,
		];
		$allowed['clippath']        = [ 'id' => true ];
		$allowed['mask']            = [ 'id' => true ];

		return wp_kses( $svg_code, $allowed );
	}

	public function start_badge_output_buffer( $element ) {
		$supported = in_array( $element->get_type(), [ 'widget', 'section', 'column', 'container' ], true );

		if ( ! $supported || ! $this->is_badge_enabled( $element ) || ! $this->should_inject_badge_via_php() ) {
			return;
		}

		self::$buffering_element_ids[] = $element->get_id();
		ob_start();
	}

	public function end_badge_output_buffer_and_inject( $element ) {
		if ( empty( self::$buffering_element_ids ) ) {
			return;
		}

		$buffering_id = self::$buffering_element_ids[ count( self::$buffering_element_ids ) - 1 ];

		if ( $buffering_id !== $element->get_id() ) {
			return;
		}

		array_pop( self::$buffering_element_ids );

		$html = ob_get_clean();

		if ( '' === $html ) {
			echo $html;
			return;
		}

		ob_start();
		$this->render_badge_markup( $element );
		$badge = ob_get_clean();

		echo $this->inject_badge_before_wrapper_close( $html, $element, $badge );
	}

	protected function inject_badge_before_wrapper_close( $html, $element, $badge ) {
		if ( '' === $badge ) {
			return $html;
		}

		if ( $element instanceof Widget_Base ) {
			return $this->inject_badge_into_widget_html( $html, $badge );
		}

		return $this->inject_badge_into_element_html( $html, $badge, $element );
	}

	/**
	 * Widget (data-element_type="widget"): inject badge inside the widget element.
	 */
	protected function inject_badge_into_widget_html( $html, $badge ) {
		if ( preg_match( '#<div[^>]*\belementor-widget-container\b#', $html ) ) {
			$updated = preg_replace( '#(\s*)(</div>)(\s*)(</div>\s*)$#s', '$1' . $badge . '$2$3$4', $html, 1 );

			if ( is_string( $updated ) && $updated !== $html ) {
				return $updated;
			}

			$updated = preg_replace( '#(\s*)(</div>)(\s*)$#s', '$1' . $badge . '$2$3', $html, 1 );

			if ( is_string( $updated ) && $updated !== $html ) {
				return $updated;
			}
		}

		return preg_replace( '#(\s*)(</div>)(\s*)$#s', '$1' . $badge . '$2$3', $html, 1 ) ?: ( $html . $badge );
	}

	protected function inject_badge_into_element_html( $html, $badge, $element ) {
		$html_tag = $this->get_element_wrapper_tag( $element );

		$inner_wrappers = [
			'e-con-inner',
			'elementor-container',
			'elementor-widget-wrap',
		];

		foreach ( $inner_wrappers as $wrapper_class ) {
			if ( ! preg_match( '#<div[^>]*\b' . preg_quote( $wrapper_class, '#' ) . '\b#', $html ) ) {
				continue;
			}

			$pattern = '#(\s*)(</div>)(\s*)(</' . preg_quote( $html_tag, '#' ) . '>\s*)$#s';
			$updated = preg_replace( $pattern, '$1' . $badge . '$2$3$4', $html, 1 );

			if ( is_string( $updated ) && $updated !== $html ) {
				return $updated;
			}
		}

		$pattern = '#(\s*</' . preg_quote( $html_tag, '#' ) . '>\s*)$#s';

		return preg_replace( $pattern, $badge . '$1', $html, 1 ) ?: ( $html . $badge );
	}

	protected function get_element_wrapper_tag( $element ) {
		if ( $element instanceof Container ) {
			$html_tag = $element->get_settings( 'html_tag' );

			return $html_tag ? $html_tag : 'div';
		}

		if ( method_exists( $element, 'get_html_tag' ) ) {
			return $element->get_html_tag();
		}

		return 'div';
	}

	protected function register_element_hooks() {
		$elements = [
			'container' => 'section_layout_container/after_section_end',
			'section'   => 'section_advanced/after_section_end',
			'column'    => 'section_advanced/after_section_end',
			'common'    => '_section_style/after_section_end',
		];

		foreach ( $elements as $element_name => $section_hook ) {
			add_action(
				'elementor/element/' . $element_name . '/' . $section_hook,
				[ $this, 'register_section' ]
			);
			add_action(
				'elementor/element/' . $element_name . '/section_element_pack_global_badge/before_section_end',
				[ $this, 'register_controls' ],
				10,
				2
			);
		}
	}

	protected function register_render_hooks() {
		add_action( 'elementor/frontend/before_render', [ $this, 'before_render' ], 10, 1 );
		// Notation-style: enqueue when element renders on frontend.
		add_action( 'elementor/frontend/before_render', [ $this, 'should_script_enqueue' ], 10, 1 );

		$buffered_types = [ 'widget', 'container', 'section', 'column' ];

		foreach ( $buffered_types as $type ) {
			add_action(
				'elementor/frontend/' . $type . '/before_render',
				[ $this, 'start_badge_output_buffer' ],
				0,
				1
			);
			add_action(
				'elementor/frontend/' . $type . '/after_render',
				[ $this, 'end_badge_output_buffer_and_inject' ],
				0,
				1
			);
		}
	}

	protected function add_actions() {
		add_action( 'wp_enqueue_scripts', [ $this, 'register_assets' ], 9 );

		$this->register_element_hooks();
		$this->register_render_hooks();

		// Notation-style: always load handler + styles in Elementor preview iframe (editor canvas).
		add_action( 'elementor/preview/enqueue_scripts', [ $this, 'enqueue_scripts' ], 20 );
		add_action( 'elementor/preview/enqueue_styles', [ $this, 'enqueue_scripts' ], 20 );
	}
}
