(function ($) {
	"use strict";

	var ecafeTiltEffect = function ($scope) {
		var tiltElements = $scope.find( '.ec-tilt' );

		if (tiltElements.length) {
			tiltElements.each(
				function () {
					var $this = $( this );

					const getData = (attr, fallback) => {
						const val = $this.data( attr );
						if (val === undefined) {
							return fallback;
						}
						if (val === "true") {
							return true;
						}
						if (val === "false") {
							return false;
						}
						if ( ! isNaN( val )) {
							return parseFloat( val );
						}
						return val;
					};

					$this.tilt(
						{
							maxTilt: getData( "tilt-max", 35 ),
							perspective: getData( "tilt-perspective", 1000 ),
							easing: getData( "easing", "cubic-bezier(.03,.98,.52,.99)" ),
							scale: getData( "tilt-scale", 1 ),
							speed: getData( "tilt-speed", 300 ),
							transition: getData( "tilt-transition", true ),
							disableAxis: getData( "tilt-axis", null ),
							reset: getData( "tilt-reset", true ),
							glare: getData( "tilt-glare", false ),
							"max-glare": getData( "tilt-max-glare", 0.5 )
						}
					);
				}
			);
		}
	};

	$( window ).on(
		'elementor/frontend/init',
		function () {
			elementorFrontend.hooks.addAction( 'frontend/element_ready/widget', ecafeTiltEffect );
		}
	);
})( jQuery );
