(function($){
    $(window).on('elementor/frontend/init', function(){
        var ecafeConfettieffect = function($scope){
            
            $scope.find(".ec-confettieffect").each(function () {
                const jsConfetti = new JSConfetti();
                let element = this;
                
                let confettiData = JSON.parse(element.getAttribute("data-ec-confettieffect") || "{}");
                let triggerType = confettiData.ecceTrigger;
                let eccecon = confettiData.eccecon;
                let eccecolor = confettiData.eccecolor;
                let emojiSize = confettiData.emojiSize;
                let confettiRadius = confettiData.confettiRadius;
                let confettiNumber = confettiData.confettiNumber;

                function triggerConfetti() {
                    let hasValidEmojis = eccecon.some(e => e.trim() !== "");
                    jsConfetti.addConfetti({
                        emojis: hasValidEmojis ? eccecon : [],
                        confettiColors: eccecolor,
                        emojiSize: emojiSize,
                        confettiRadius: confettiRadius,
                        confettiNumber: confettiNumber
                    });
                }

                if (triggerType === "load") {
                    triggerConfetti();
                } else if (triggerType === "click") {
                    element.addEventListener("click", () => {
                        triggerConfetti();
                    });
                }else if (triggerType === "hover") {
                    element.addEventListener("mouseenter", () => {
                        triggerConfetti();
                    });
                }
            });
        };
        
        elementorFrontend.hooks.addAction("frontend/element_ready/section", ecafeConfettieffect);
        elementorFrontend.hooks.addAction("frontend/element_ready/container", ecafeConfettieffect);
        
    });
})(jQuery);