/**
 * RSM Event Countdown — client-side tick.
 */
( function () {
	'use strict';

	function prefersReducedMotion() {
		return (
			window.matchMedia &&
			window.matchMedia( '(prefers-reduced-motion: reduce)' ).matches
		);
	}

	function pad( value ) {
		return String( Math.max( 0, parseInt( value, 10 ) || 0 ) );
	}

	function unitsFromDiff( diffMs ) {
		var totalSeconds = Math.max( 0, Math.floor( diffMs / 1000 ) );
		var days = Math.floor( totalSeconds / 86400 );
		totalSeconds %= 86400;
		var hours = Math.floor( totalSeconds / 3600 );
		totalSeconds %= 3600;
		var minutes = Math.floor( totalSeconds / 60 );
		var seconds = totalSeconds % 60;
		return { days: days, hours: hours, minutes: minutes, seconds: seconds };
	}

	function hideHost( host ) {
		host.remove();
	}

	function setExpired( host, message ) {
		if ( host.getAttribute( 'data-hide-when-expired' ) === '1' ) {
			hideHost( host );
			return;
		}

		host.classList.add( 'rsmspark-cw-event-countdown--expired' );
		var display = host.querySelector( '.rsmspark-cw-event-countdown__display' );
		if ( display ) {
			display.remove();
		}
		var staticEl = host.querySelector( '.rsmspark-cw-event-countdown__static' );
		if ( staticEl ) {
			staticEl.remove();
		}
		var expired = host.querySelector( '.rsmspark-cw-event-countdown__expired' );
		if ( ! expired ) {
			expired = document.createElement( 'p' );
			expired.className = 'rsmspark-cw-event-countdown__expired';
			host.appendChild( expired );
		}
		expired.textContent = message || 'Countdown expired';
	}

	function updateHost( host ) {
		var endTs = parseInt( host.getAttribute( 'data-end-ts' ), 10 );
		if ( ! endTs ) {
			return false;
		}

		var diff = endTs * 1000 - Date.now();
		if ( diff <= 0 ) {
			setExpired( host, host.getAttribute( 'data-expired-message' ) || '' );
			return false;
		}

		var units = unitsFromDiff( diff );
		Object.keys( units ).forEach( function ( key ) {
			var node = host.querySelector( '[data-unit="' + key + '"]' );
			if ( node ) {
				node.textContent = pad( units[ key ] );
			}
		} );
		return true;
	}

	function initHost( host ) {
		if ( ! host || host.getAttribute( 'data-rsmspark-cw-countdown-init' ) === '1' ) {
			return;
		}

		host.setAttribute( 'data-rsmspark-cw-countdown-init', '1' );

		if ( host.classList.contains( 'rsmspark-cw-event-countdown--expired' ) ) {
			return;
		}

		if ( prefersReducedMotion() ) {
			return;
		}

		if ( ! updateHost( host ) ) {
			return;
		}

		var interval = window.setInterval( function () {
			if ( ! updateHost( host ) ) {
				window.clearInterval( interval );
			}
		}, 1000 );
	}

	function initAll( scope ) {
		var root = scope && scope.querySelectorAll ? scope : document;
		root.querySelectorAll( '[data-rsmspark-cw-event-countdown]' ).forEach( initHost );
	}

	window.rsmEventCountdownInit = initAll;

	if ( document.readyState === 'loading' ) {
		document.addEventListener( 'DOMContentLoaded', function () {
			initAll( document );
		} );
	} else {
		initAll( document );
	}
} )();
