(function () {
	'use strict';

	var modal = document.querySelector('[data-role-permissions-modal]');
	if (!modal) {
		return;
	}

	var modalBody = modal.querySelector('[data-role-permissions-modal-body]');
	var modalTitle = modal.querySelector('[data-role-permissions-modal-title]');
	var modalSubtitle = modal.querySelector('[data-role-permissions-modal-subtitle]');
	var activeTrigger = null;
	var activeSource = null;
	var activePlaceholder = null;

	function getSummaryText(picker) {
		var total = picker.getAttribute('data-panel-total') || '0';
		var checked = picker.querySelectorAll('input[type="checkbox"]:checked').length;
		var template = modal.getAttribute('data-summary-format') || '%1$d de %2$d activas';
		return template.replace('%1$d', String(checked)).replace('%2$d', String(total));
	}

	function updateTriggerSummary(trigger, picker) {
		var summary = trigger.querySelector('[data-role-permissions-summary]');
		if (summary) {
			summary.textContent = getSummaryText(picker);
		}
	}

	function closeModal() {
		if (!modal.hasAttribute('hidden')) {
			modal.setAttribute('hidden', 'hidden');
		}

		document.body.classList.remove('logistica-role-permissions-modal-open');

		if (activeSource && activePlaceholder && activePlaceholder.parentNode) {
			activeSource.setAttribute('hidden', 'hidden');
			activePlaceholder.parentNode.insertBefore(activeSource, activePlaceholder);
			activePlaceholder.remove();
		}

		if (activeTrigger) {
			activeTrigger.setAttribute('aria-expanded', 'false');
		}

		activeTrigger = null;
		activeSource = null;
		activePlaceholder = null;
		modalBody.innerHTML = '';
	}

	function openModal(trigger) {
		var sourceId = trigger.getAttribute('aria-controls');
		var source = sourceId ? document.getElementById(sourceId) : null;
		if (!source) {
			return;
		}

		closeModal();

		var picker = source.querySelector('[data-role-permissions-picker]');
		if (!picker) {
			return;
		}

		activeTrigger = trigger;
		activeSource = source;
		activePlaceholder = document.createComment('logistica-role-permissions-placeholder');
		source.parentNode.insertBefore(activePlaceholder, source);

		if (modalTitle) {
			modalTitle.textContent = trigger.getAttribute('data-role-label') || '';
		}
		if (modalSubtitle) {
			modalSubtitle.textContent = trigger.getAttribute('data-panel-label') || '';
		}

		modalBody.appendChild(source);
		source.removeAttribute('hidden');
		modal.removeAttribute('hidden');
		document.body.classList.add('logistica-role-permissions-modal-open');
		trigger.setAttribute('aria-expanded', 'true');

		var firstInput = picker.querySelector('input[type="checkbox"]');
		if (firstInput) {
			firstInput.focus();
		}
	}

	modal.addEventListener('click', function (event) {
		if (event.target.closest('[data-role-permissions-close]')) {
			event.preventDefault();
			closeModal();
		}
	});

	document.addEventListener('click', function (event) {
		var trigger = event.target.closest('[data-role-permissions-open]');
		if (!trigger) {
			return;
		}

		event.preventDefault();
		openModal(trigger);
	});

	document.addEventListener('change', function (event) {
		var picker = event.target.closest('[data-role-permissions-picker]');
		if (!picker || !activeTrigger) {
			return;
		}
		updateTriggerSummary(activeTrigger, picker);
	});

	document.addEventListener('keydown', function (event) {
		if ('Escape' === event.key && !modal.hasAttribute('hidden')) {
			event.preventDefault();
			closeModal();
		}
	});
})();
