(function () {
	'use strict';

	var copy = {
		'form-login': {
			title: '1MZ For Artists',
			subtitle: 'Entra com o teu telefone e PIN para continuar.'
		},
		'form-register': {
			title: 'Criar conta',
			subtitle: 'Preenche os dados para abrir o teu perfil de artista.'
		},
		'form-recover-1': {
			title: 'Recuperar acesso',
			subtitle: 'Confirma os dados da conta para iniciares a recuperação.'
		},
		'form-recover-2': {
			title: 'Definir novo PIN',
			subtitle: 'Usa a chave de recuperação e escolhe um novo PIN.'
		}
	};

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

		var card = root.querySelector('.rmbz-auth-card');
		var title = root.querySelector('.rmbz-auth-card__title');
		var subtitle = root.querySelector('.rmbz-auth-card__subtitle');
		var tabs = Array.prototype.slice.call(root.querySelectorAll('.onemz-auth-tab'));
		var forms = Array.prototype.slice.call(root.querySelectorAll('.onemz-auth-form'));

		if (title) {
			title.setAttribute('aria-live', 'polite');
		}

		tabs.forEach(function (tab, index) {
			var controlledForm = index === 0 ? 'form-login' : 'form-register';
			tab.setAttribute('aria-controls', controlledForm);

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

			tab.addEventListener('click', function () {
				window.setTimeout(sync, 0);
			});
		});

		function sync() {
			var active = forms.find(function (form) {
				return form.classList.contains('active');
			});

			forms.forEach(function (form) {
				var isActive = form === active;
				form.setAttribute('aria-hidden', isActive ? 'false' : 'true');
				if ('inert' in form) {
					form.inert = !isActive;
				}
			});

			if (!active) {
				return;
			}

			var content = copy[active.id] || copy['form-login'];
			if (title) {
				title.textContent = content.title;
			}
			if (subtitle) {
				subtitle.textContent = content.subtitle;
			}
			if (card) {
				card.dataset.authMode = active.id.replace('form-', '');
			}
		}

		var observer = new MutationObserver(function (mutations) {
			if (mutations.some(function (mutation) {
				return mutation.type === 'attributes' && mutation.attributeName === 'class';
			})) {
				sync();
			}
		});

		forms.forEach(function (form) {
			observer.observe(form, { attributes: true, attributeFilter: ['class'] });
		});

		sync();
	}

	function initAll() {
		Array.prototype.slice.call(document.querySelectorAll('.rmbz-auth-wrapper--split')).forEach(init);
	}

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