(function () {
	'use strict';

	function normalize(value) {
		var text = String(value || '').toLowerCase();
		return typeof text.normalize === 'function'
			? text.normalize('NFD').replace(/[\u0300-\u036f]/g, '')
			: text;
	}

	function initShowcase(root) {
		if (!root || root.dataset.showcaseReady === 'true') {
			return;
		}
		root.dataset.showcaseReady = 'true';

		var toast = root.querySelector('[data-showcase-toast]');
		var toastTimer = 0;

		function showToast(message) {
			if (!toast) {
				return;
			}
			window.clearTimeout(toastTimer);
			toast.textContent = message;
			toast.hidden = false;
			toastTimer = window.setTimeout(function () {
				toast.hidden = true;
			}, 3200);
		}

		var filterButtons = Array.prototype.slice.call(root.querySelectorAll('[data-feature-filter]'));
		var featureCards = Array.prototype.slice.call(root.querySelectorAll('[data-feature-card]'));

		filterButtons.forEach(function (button) {
			button.addEventListener('click', function () {
				var category = button.dataset.featureFilter;
				filterButtons.forEach(function (item) {
					item.setAttribute('aria-pressed', item === button ? 'true' : 'false');
				});
				featureCards.forEach(function (card) {
					card.hidden = category !== 'all' && card.dataset.featureCard !== category;
				});
			});
		});

		var tabs = Array.prototype.slice.call(root.querySelectorAll('[data-showcase-tab]'));
		var panels = Array.prototype.slice.call(root.querySelectorAll('[data-showcase-panel]'));

		function activateTab(tab, focus) {
			var key = tab.dataset.showcaseTab;
			tabs.forEach(function (item) {
				var selected = item === tab;
				item.setAttribute('aria-selected', selected ? 'true' : 'false');
				item.tabIndex = selected ? 0 : -1;
			});
			panels.forEach(function (panel) {
				panel.hidden = panel.dataset.showcasePanel !== key;
			});
			if (focus) {
				tab.focus();
			}
		}

		tabs.forEach(function (tab, index) {
			tab.addEventListener('click', function () {
				activateTab(tab, false);
			});
			tab.addEventListener('keydown', function (event) {
				var nextIndex = index;
				if (event.key === 'ArrowRight' || event.key === 'ArrowDown') {
					nextIndex = (index + 1) % tabs.length;
				} else if (event.key === 'ArrowLeft' || event.key === 'ArrowUp') {
					nextIndex = (index - 1 + tabs.length) % tabs.length;
				} else if (event.key === 'Home') {
					nextIndex = 0;
				} else if (event.key === 'End') {
					nextIndex = tabs.length - 1;
				} else {
					return;
				}
				event.preventDefault();
				activateTab(tabs[nextIndex], true);
			});
		});

		var faqInput = root.querySelector('[data-faq-search]');
		var faqClear = root.querySelector('[data-faq-clear]');
		var faqCount = root.querySelector('[data-faq-count]');
		var faqEmpty = root.querySelector('[data-faq-empty]');
		var faqItems = Array.prototype.slice.call(root.querySelectorAll('[data-faq-item]'));

		function filterFaqs() {
			var query = normalize(faqInput ? faqInput.value.trim() : '');
			var visible = 0;
			faqItems.forEach(function (item) {
				var matches = !query || normalize(item.dataset.search).indexOf(query) !== -1;
				item.hidden = !matches;
				if (!matches) {
					item.open = false;
				} else {
					visible += 1;
				}
			});
			if (faqCount) {
				faqCount.textContent = visible + (visible === 1 ? ' pergunta' : ' perguntas');
			}
			if (faqEmpty) {
				faqEmpty.hidden = visible !== 0;
			}
			if (faqClear) {
				faqClear.hidden = query.length === 0;
			}
		}

		if (faqInput) {
			faqInput.addEventListener('input', filterFaqs);
			faqInput.addEventListener('keydown', function (event) {
				if (event.key === 'Escape' && faqInput.value) {
					faqInput.value = '';
					filterFaqs();
				}
			});
		}

		if (faqClear && faqInput) {
			faqClear.addEventListener('click', function () {
				faqInput.value = '';
				filterFaqs();
				faqInput.focus();
			});
		}

		Array.prototype.slice.call(root.querySelectorAll('a[href^="#"]')).forEach(function (link) {
			link.addEventListener('click', function (event) {
				var id = link.getAttribute('href').slice(1);
				var target = document.getElementById(id);
				if (!target) {
					return;
				}
				event.preventDefault();
				target.scrollIntoView({
					behavior: window.matchMedia('(prefers-reduced-motion: reduce)').matches ? 'auto' : 'smooth',
					block: 'start'
				});
				if (window.history && window.history.replaceState) {
					window.history.replaceState(null, '', '#' + id);
				}
			});
		});

		var serviceDialog = root.querySelector('[data-service-dialog]');
		var serviceOpeners = Array.prototype.slice.call(root.querySelectorAll('[data-service-open]'));
		var servicePanels = Array.prototype.slice.call(root.querySelectorAll('[data-service-detail]'));
		var serviceClose = root.querySelector('[data-service-close]');
		var serviceHeading = root.querySelector('[data-service-modal-heading]');
		var lastServiceTrigger = null;

		function finishServiceDialog() {
			document.documentElement.classList.remove('onemz-showcase-dialog-open');
			servicePanels.forEach(function (panel) {
				panel.hidden = true;
			});
			if (lastServiceTrigger) {
				lastServiceTrigger.focus();
				lastServiceTrigger = null;
			}
		}

		function closeServiceDialog() {
			if (!serviceDialog) {
				return;
			}
			if (typeof serviceDialog.close === 'function' && serviceDialog.open) {
				serviceDialog.close();
			} else {
				serviceDialog.removeAttribute('open');
				finishServiceDialog();
			}
		}

		function openServiceDialog(trigger) {
			if (!serviceDialog) {
				return;
			}
			var key = trigger.dataset.serviceOpen;
			var activePanel = servicePanels.find(function (panel) {
				return panel.dataset.serviceDetail === key;
			});
			if (!activePanel) {
				return;
			}

			servicePanels.forEach(function (panel) {
				panel.hidden = panel !== activePanel;
			});
			var panelTitle = activePanel.querySelector('h3');
			if (serviceHeading && panelTitle) {
				serviceHeading.textContent = panelTitle.textContent;
			}
			if (panelTitle && panelTitle.id) {
				serviceDialog.setAttribute('aria-labelledby', panelTitle.id);
			}

			lastServiceTrigger = trigger;
			document.documentElement.classList.add('onemz-showcase-dialog-open');
			var content = serviceDialog.querySelector('.onemz-showcase__service-dialog-content');
			if (content) {
				content.scrollTop = 0;
			}

			if (typeof serviceDialog.showModal === 'function') {
				if (!serviceDialog.open) {
					serviceDialog.showModal();
				}
			} else {
				serviceDialog.setAttribute('open', '');
			}
		}

		serviceOpeners.forEach(function (trigger) {
			trigger.addEventListener('click', function () {
				openServiceDialog(trigger);
			});
		});

		if (serviceClose) {
			serviceClose.addEventListener('click', closeServiceDialog);
		}

		if (serviceDialog) {
			serviceDialog.addEventListener('click', function (event) {
				if (event.target === serviceDialog) {
					closeServiceDialog();
				}
			});
			serviceDialog.addEventListener('close', finishServiceDialog);
			serviceDialog.addEventListener('cancel', function () {
				document.documentElement.classList.remove('onemz-showcase-dialog-open');
			});
			serviceDialog.addEventListener('keydown', function (event) {
				if (event.key === 'Escape' && typeof serviceDialog.showModal !== 'function') {
					event.preventDefault();
					closeServiceDialog();
				}
			});
		}
		var shareButton = root.querySelector('[data-showcase-share]');
		if (shareButton) {
			shareButton.addEventListener('click', function () {
				var data = {
					title: document.title || '1MZ For Artists',
					text: 'Conhece o 1MZ For Artists.',
					url: window.location.href
				};

				if (navigator.share) {
					navigator.share(data).catch(function (error) {
						if (!error || error.name !== 'AbortError') {
							showToast('Não foi possível abrir a partilha.');
						}
					});
					return;
				}

				if (navigator.clipboard && navigator.clipboard.writeText) {
					navigator.clipboard.writeText(data.url).then(function () {
						showToast('Ligação copiada.');
					}).catch(function () {
						showToast('Não foi possível copiar a ligação.');
					});
					return;
				}

				var helper = document.createElement('textarea');
				helper.value = data.url;
				helper.setAttribute('readonly', '');
				helper.style.position = 'fixed';
				helper.style.opacity = '0';
				document.body.appendChild(helper);
				helper.select();
				try {
					document.execCommand('copy');
					showToast('Ligação copiada.');
				} catch (error) {
					showToast('Não foi possível copiar a ligação.');
				}
				document.body.removeChild(helper);
			});
		}
	}

	function initAll() {
		Array.prototype.slice.call(document.querySelectorAll('[data-showcase-root]')).forEach(initShowcase);
	}

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