( function( $ ) {
    var pxl_widget_particle_handler = function( $scope, $ ) {

        setTimeout(function() {
            if ($scope.find('.elementor-container .elementor-widget-pxl_particle').length) {
                var _el_particle = $scope.find(".elementor-container .elementor-widget-pxl_particle"),
                _original_particle = _el_particle.clone();
                _el_particle.addClass('original');
                $scope.prepend(_original_particle);
                if (window.elementorFrontend.isEditMode()) {
                    _el_particle.addClass('pxl-hidden');
                    var observer = new MutationObserver(function(mutations) {
                        mutations.forEach(function(mutation) {
                            var _original_particle_edit = $scope.find(".elementor-container .elementor-widget-pxl_particle.original").clone();
                            $scope.find("> .elementor-widget-pxl_particle").remove();
                            if (!$scope.find('> .elementor-widget-pxl_particle.original').length) {
                                _original_particle_edit.removeClass('pxl-hidden');
                                $scope.prepend(_original_particle_edit);
                            }
                        });
                    });
                    var config = { childList: true, subtree: true };
                    observer.observe(_el_particle[0], config);
                } else {
                    _el_particle.remove();
                }
            }
        }, 100);

    };

    $( window ).on( 'elementor/frontend/init', function() {
        elementorFrontend.hooks.addAction( 'frontend/element_ready/global', pxl_widget_particle_handler );
        elementorFrontend.hooks.addAction( 'frontend/element_ready/pxl_particle.default', pxl_widget_particle_handler );
    } );
} )( jQuery );