window.deferredPrompt = null;
const divInstall	= document.getElementById('installContainer');
const installButton = document.getElementById('installButton');
const closeButton	= document.getElementById('closeButton');

window.addEventListener
('beforeinstallprompt', 
	(event) => 
	{
		var ari = getCookie('appRefuseInstall');
		if (!ari)
		{
			console.log('👍', 'beforeinstallprompt', event);
			// Stash the event so it can be triggered later.
			window.deferredPrompt = event;
			// Remove the 'hide' class from the install button container
			divInstall.classList.toggle('hide', false);
		}
	}
);

installButton.addEventListener
('click', () => 
	{
		console.log('👍', 'installButton-clicked');
		divInstall.classList.toggle('hide', true);
		const promptEvent = window.deferredPrompt;
		if (!promptEvent) 
		{
			// The deferred prompt isn't available.
			return;
		}
		// Show the install prompt.
		promptEvent.prompt();
		// Log the result
		promptEvent.userChoice.then
		(
			(result) => 
			{
				console.log('👍', 'userChoice', result);
				// Reset the deferred prompt variable, since
				// prompt() can only be called once.
				window.deferredPrompt = null;
				// Hide the install button.
				divInstall.classList.toggle('hide', true);
				setCookie('appRefuseInstall', true, 21);
			}
		);
	}
);

closeButton.addEventListener
('click', () => 
	{
		console.log('👍', 'userChoice', 'closing installer');
		setCookie('appRefuseInstall', true, 21);
		divInstall.classList.toggle('hide', true);
	}
)