(function ($) {
	"use strict";
	var WidgetChangeLog = function ($scope, $) {
		let container = $scope[0].querySelectorAll( '.eca-changelog.one-by-one-anim' );

		if ( container.length ) {
			let winHeight = window.innerHeight,
				eletop    = container[0].getBoundingClientRect().top,
				eleshow   = 150;

			if (eletop < winHeight - eleshow) {
				ec_cl_one_by_one_animation();
			} else {
				window.addEventListener( "scroll", ec_cl_one_by_one_animation );
			}

			function ec_cl_one_by_one_animation(){
				let Lists = container[0].querySelectorAll( '.eca-changelog-list' ),
					gap   = container[0].getAttribute( 'data-gap' ),
					delay = container[0].getAttribute( 'data-delay' );

				let winHeight = window.innerHeight,
					eletop    = container[0].getBoundingClientRect().top,
					eleshow   = 150;

				if (eletop < winHeight - eleshow) {
					Lists.forEach(
						function (self, id) {
							setTimeout(
								function () {
									fadeInEffect( self, Number( delay ) );
								},
								id * Number( gap )
							);
						}
					);

					window.removeEventListener( "scroll", ec_cl_one_by_one_animation );
				}
			}

			function fadeInEffect(element, duration=600) {
				element.style.display = '';
				element.style.opacity = 0;
				var last              = +new Date();
				var tick              = function () {
					element.style.opacity = +element.style.opacity + (new Date() - last) / duration;
					last                  = +new Date();
					if (+element.style.opacity < 1) {
						(window.requestAnimationFrame && requestAnimationFrame( tick )) || setTimeout( tick, 16 );
					}
				};
				tick();
			}
		}
	};

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