/**
 * EasySite Pro – Popup builder triggers
 */
(function () {
	'use strict';

	var popup = document.getElementById('easysite-popup');
	if (!popup) return;

	var trigger = popup.getAttribute('data-trigger') || 'delay';
	var shown = false;
	var storageKey = 'easysite_popup_dismissed';

	function showPopup() {
		if (shown || sessionStorage.getItem(storageKey)) return;
		popup.hidden = false;
		shown = true;
	}

	function hidePopup() {
		popup.hidden = true;
		sessionStorage.setItem(storageKey, '1');
	}

	popup.querySelectorAll('[data-close]').forEach(function (el) {
		el.addEventListener('click', hidePopup);
	});

	switch (trigger) {
		case 'load':
			showPopup();
			break;
		case 'delay':
			setTimeout(showPopup, 3000);
			break;
		case 'scroll':
			window.addEventListener('scroll', function onScroll() {
				if (window.scrollY > 400) {
					showPopup();
					window.removeEventListener('scroll', onScroll);
				}
			}, { passive: true });
			break;
		case 'exit':
			document.addEventListener('mouseout', function (e) {
				if (e.clientY < 10) showPopup();
			});
			break;
	}
})();
