(function ($) {
	"use strict";
	var WidgetInfoCircle = function ($scope, $) {
		if ($scope.find( '.ec-info-circle-wrapper' ).length) {
			var icons       = $scope.find( ".ec-ic-loop-image-icon" );
			var radius      = 48; // Radius of the circle
			var center      = 50; // Center point (percentage)
			var startAngle  = -90; // Start angle (top of the circle)
			var totalDivide = 360 / icons.length; // Angle increment for each icon
			var settings    = $scope.find( '.ec-info-circle-wrapper' ).data( 'settings' );

			// Desktop positioning
			icons.each(
				function (index) {
					var angle = (startAngle + index * totalDivide) * (Math.PI / 180); // Convert to radians
					var x     = center + radius * Math.cos( angle );
					var y     = center + radius * Math.sin( angle );

					// Set calculated positions
					$( this ).css(
						{
							left: x + "%",
							top: y + "%",
							"margin-left": -0.5 * $( this ).outerWidth(),
							"margin-top": -0.5 * $( this ).outerHeight()
						}
					);

					// Use jQuery's on() method to bind the event
					if (settings.icTrigger == 'click') {
						$( this ).on(
							settings.icTrigger,
							function (event) {
								$( this ).closest( '.ec-info-circle-wrapper' ).find( '.ec-ic-active-item' ).removeClass( 'ec-ic-active-item' );
								$( this ).closest( '.ec-ic-loop-item' ).addClass( 'ec-ic-active-item' );
							}
						);
					} else if (settings.icTrigger == 'hover') {
						$( this ).hover(
							function () {
								$( this ).closest( '.ec-info-circle-wrapper' ).find( '.ec-ic-active-item' ).removeClass( 'ec-ic-active-item' );
								$( this ).closest( '.ec-ic-loop-item' ).addClass( 'ec-ic-active-item' );
							}
						);
					}
				}
			);

			// Set wrapper opacity to 1 for proper visibility
			icons.closest( '.ec-info-circle-wrapper' ).css( 'opacity', '1' );

			if (settings.icAutoplay == 'yes') {
				setInterval(
					function () {
						if ($scope.find( ".ec-ic-active-item" ).next().length > 0) {
							$scope.find( ".ec-ic-active-item" ).next().addClass( "ec-ic-active-item" ).siblings().removeClass( "ec-ic-active-item" );
						} else {
							$scope.find( ".ec-ic-loop-item" ).eq( 0 ).addClass( "ec-ic-active-item" ).siblings().removeClass( "ec-ic-active-item" );
						}
					},
					settings.icAutoplayDuration
				);

			}

		}
	};

	$( window ).on(
		'elementor/frontend/init',
		function () {
			elementorFrontend.hooks.addAction( 'frontend/element_ready/ecafe-infocircle.default', WidgetInfoCircle );
		}
	);
})( jQuery );
