(function () {
	'use strict';

	document.addEventListener('DOMContentLoaded', initFormsCommandCenter);

	function initFormsCommandCenter() {
		var table = document.querySelector('.subin-forms-table');
		var bulkForm = document.querySelector('#subin-forms-bulk');

		if (!table || !bulkForm) {
			return;
		}

		var selectAll = table.querySelector('[data-subin-select-all]');
		var checkboxes = Array.prototype.slice.call(table.querySelectorAll('[data-subin-form-select]'));
		var action = bulkForm.querySelector('[data-subin-bulk-action]');
		var submit = bulkForm.querySelector('[data-subin-bulk-submit]');
		var count = bulkForm.querySelector('[data-subin-selected-count]');

		function updateSelection() {
			var selected = checkboxes.filter(function (checkbox) { return checkbox.checked; }).length;
			if (selectAll) {
				selectAll.checked = selected > 0 && selected === checkboxes.length;
				selectAll.indeterminate = selected > 0 && selected < checkboxes.length;
			}
			if (count) {
				count.textContent = selected + ' selected';
			}
			if (submit) {
				submit.disabled = selected === 0 || !action || !action.value;
			}
		}

		function updateBulkTarget() {
			var value = action ? action.value : '';
			var folder = bulkForm.querySelector('[data-subin-bulk-target="folder"]');
			var tag = bulkForm.querySelector('[data-subin-bulk-target="tag"]');

			if (folder) {
				folder.hidden = value !== 'move_folder';
			}
			if (tag) {
				tag.hidden = value !== 'add_tag' && value !== 'remove_tag';
			}
			updateSelection();
		}

		if (selectAll) {
			selectAll.addEventListener('change', function () {
				checkboxes.forEach(function (checkbox) { checkbox.checked = selectAll.checked; });
				updateSelection();
			});
		}

		checkboxes.forEach(function (checkbox) {
			checkbox.addEventListener('change', updateSelection);
		});

		if (action) {
			action.addEventListener('change', updateBulkTarget);
		}

		bulkForm.addEventListener('submit', function (event) {
			var dangerous = action && (action.value === 'trash' || action.value === 'delete_permanently');
			var confirmation = action && action.value === 'delete_permanently'
				? bulkForm.getAttribute('data-subin-permanent-confirm')
				: bulkForm.getAttribute('data-subin-trash-confirm');
			if (dangerous && !window.confirm(confirmation)) {
				event.preventDefault();
			}
		});

		table.addEventListener('click', function (event) {
			var toggle = event.target.closest('[data-subin-toggle-details]');
			if (!toggle) {
				return;
			}

			var details = document.getElementById(toggle.getAttribute('aria-controls'));
			if (!details) {
				return;
			}

			var expanded = toggle.getAttribute('aria-expanded') === 'true';
			toggle.setAttribute('aria-expanded', expanded ? 'false' : 'true');
			toggle.setAttribute('title', expanded ? 'Open details' : 'Close details');
			details.hidden = expanded;
		});

		document.addEventListener('click', function (event) {
			document.querySelectorAll('.subin-row-menu[open], .subin-organize-menu[open]').forEach(function (menu) {
				if (!menu.contains(event.target)) {
					menu.removeAttribute('open');
				}
			});
		});

		updateBulkTarget();
	}
})();
