(function ($) {
	"use strict";

	const getData = ($el, attr, fallback) => {
		let val   = $el.data( attr );

		if (val === undefined || val === "") {
			return fallback;
		}

		if (typeof val === "string") {
			val = val.toLowerCase();
			if (val === "true" || val === "yes") {
				return true;
			}
			if (val === "false" || val === "no" ) {
				return false;
			}
		}

		if ( ! isNaN( val ) && val !== "") {
			return parseFloat( val );
		}

		return val;
	};

	const ecafeTippyTooltip = function ($scope) {
		const tippyElements = $scope.find( '.ec-tippy-tooltip' );

		if (tippyElements.length) {
			tippyElements.each(
				function () {
					const $this = $( this );
					// Prevent duplicate init
					if (this._tippy) {
						return;
					}
					tippy(
						$this.get( 0 ),
						{
							content: getData( $this, 'tippy-content', '' ),
							theme: getData( $this, 'tippy-theme', 'light' ),
							placement: getData( $this, 'tippy-placements', 'top' ),
							offset: [getData( $this, 'tippy-skid', 0 ), getData( $this, 'tippy-distance', 0 )],
							arrow: getData( $this, 'tippy-arrows', false ),
							animation: getData( $this, 'tippy-animations', 'fade' ),
							trigger: getData( $this, 'tippy-triggers', 'mouseenter' ),
							//appendTo: 'parent',
							appendTo: document.body,
							interactive: getData( $this, 'tippy-interactivity', false ),
							delay: [
							getData( $this, 'tippy-induration', 0 ),
							getData( $this, 'tippy-outduration', 0 )
							],
							followCursor: getData( $this, 'tippy-follow-cursor', false )
						}
					);
				}
			);
		}
	};

	$( window ).on(
		'elementor/frontend/init',
		function () {
			elementorFrontend.hooks.addAction( 'frontend/element_ready/widget', function($scope){

				ecafeTippyTooltip($scope);
	
				// Re-init after Isotope relayout
				setTimeout(function(){
					ecafeTippyTooltip($scope);
				},500);
	
			} );
		}
	);
})( jQuery );