(function ($) {
	var WidgetCountDown = function ($scope, $) {
		let day     = $scope[0].querySelector( '#ec-cd-day' ),
			hours   = $scope[0].querySelector( '#ec-cd-hours' ),
			minutes = $scope[0].querySelector( '#ec-cd-minutes' ),
			seconds = $scope[0].querySelector( '#ec-cd-seconds' ),
			date    = $scope[0].querySelector( '.ec-countdown-wrapper-main' ).dataset.date,
			expire  = $scope[0].querySelector( '.ec-countdown-wrapper-main' ).dataset.expire;

			let getdate = date.split( "," ),
				a1      = parseInt( getdate[0] ),
				a2      = parseInt( getdate[1] ),
				a3      = parseInt( getdate[2] ),
				a4      = parseInt( getdate[3] ),
				a5      = parseInt( getdate[4] ),
				a6      = parseInt( getdate[5] );

			let enddate = '';
		if (a1.length != 0 && a2.length != 0 && a3.length != 0 && a4.length != 0 && a5.length != 0 && a6.length != 0 && a1 != undefined && a2 != undefined && a3 != undefined && a4 != undefined && a5 != undefined && a6 != undefined) {
			enddate = new Date( a1,a2,a3,a4,a5,a6 );
			// new Date(year, month, day, hours, minutes, seconds, milliseconds)
			let endtime = enddate.getTime();
			function ec_countdown() {
				let currentDate   = new Date();
				let currentTime   = currentDate.getTime();
				let remainingTime = endtime - currentTime;
				let oneminutes    = 60 * 1000;
				let onehours      = 60 * oneminutes;
				let oneday        = 24 * onehours;

				let prefixzero = (num) => (num < 10 ? `0${num}` : num);

				if (endtime < currentTime) {
					clearInterval( i );
					$scope[0].querySelector( ".ec-countdown-inner" ).innerHTML = expire;
				} else {
					let dayleft    = Math.floor( remainingTime / oneday );
					let hourleft   = Math.floor( (remainingTime % oneday) / onehours );
					let minuteleft = Math.floor( (remainingTime % onehours) / oneminutes );
					let secondleft = Math.floor( (remainingTime % oneminutes) / 1000 );

					day.textContent     = prefixzero( dayleft );
					hours.textContent   = prefixzero( hourleft );
					minutes.textContent = prefixzero( minuteleft );
					seconds.textContent = prefixzero( secondleft );
				}
			}

			let i = setInterval( ec_countdown, 1000 );
			ec_countdown();
		}
	};
	$( window ).on(
		'elementor/frontend/init',
		function () {
			elementorFrontend.hooks.addAction( 'frontend/element_ready/ecafe-countdown.default', WidgetCountDown );
		}
	);
})( jQuery );