(function (window, document) {
	'use strict';

	function relocateExternalNotices() {
		var wrapper = document.querySelector('.tmsmtp-wrap');
		if (!wrapper || !wrapper.parentNode) {
			return;
		}

		wrapper.querySelectorAll('.fs-notice,.notice,.updated,.error,.update-nag').forEach(function (notice) {
			wrapper.parentNode.insertBefore(notice, wrapper);
		});
	}

	function observeExternalNotices() {
		var wrapper = document.querySelector('.tmsmtp-wrap');
		relocateExternalNotices();
		if (!wrapper || !window.MutationObserver) {
			return;
		}

		new window.MutationObserver(relocateExternalNotices).observe(wrapper, {
			childList: true,
			subtree: true
		});
	}

	function syncProviderRows(select) {
		if (!select) {
			return;
		}

		var form = select.closest('form') || document;
		var mailer = select.value;
		form.querySelectorAll('.tmsmtp-provider-row').forEach(function (row) {
			row.style.display = row.classList.contains('tmsmtp-mailer-' + mailer) ? 'table-row' : 'none';
		});
	}

	function initializeProviderRows() {
		document.querySelectorAll('.tmsmtp-mailer-select').forEach(function (select) {
			select.addEventListener('change', function () {
				syncProviderRows(select);
			});
			syncProviderRows(select);
		});
	}

	function resetBusyForms() {
		document.querySelectorAll('.tmsmtp-wrap form[data-tmsmtp-submitting="1"]').forEach(function (form) {
			delete form.dataset.tmsmtpSubmitting;
			form.removeAttribute('aria-busy');
			form.classList.remove('tmsmtp-form-busy');
			form.querySelectorAll('.tmsmtp-btn').forEach(function (button) {
				button.disabled = false;
				button.removeAttribute('aria-disabled');
				button.classList.remove('is-loading');
			});
			form.querySelectorAll('.tmsmtp-button-spinner,.tmsmtp-button-status').forEach(function (element) {
				element.remove();
			});
		});
	}

	function initializeForms() {
		var processingText = window.tmsmtpAdmin && window.tmsmtpAdmin.processingText
			? window.tmsmtpAdmin.processingText
			: 'Processing…';

		document.addEventListener('click', function (event) {
			var button = event.target.closest('.tmsmtp-result-dismiss');
			if (!button) {
				return;
			}
			var message = button.closest('.tmsmtp-result-message');
			if (message) {
				message.remove();
			}
		});

		document.querySelectorAll('.tmsmtp-wrap form[method="post"]').forEach(function (form) {
			form.addEventListener('submit', function (event) {
				if (form.dataset.tmsmtpSubmitting === '1') {
					event.preventDefault();
					return;
				}

				var submitter = event.submitter || document.activeElement;
				if (!submitter || !submitter.matches('.tmsmtp-btn')) {
					submitter = form.querySelector('button.tmsmtp-btn[type="submit"],input.tmsmtp-btn[type="submit"]');
				}
				if (!submitter) {
					return;
				}

				event.preventDefault();
				form.dataset.tmsmtpSubmitting = '1';
				form.setAttribute('aria-busy', 'true');
				form.classList.add('tmsmtp-form-busy');

				if (submitter.name) {
					var submittedValue = document.createElement('input');
					submittedValue.type = 'hidden';
					submittedValue.name = submitter.name;
					submittedValue.value = submitter.value;
					form.appendChild(submittedValue);
				}

				form.querySelectorAll('button.tmsmtp-btn,input.tmsmtp-btn').forEach(function (actionButton) {
					actionButton.disabled = true;
					actionButton.setAttribute('aria-disabled', 'true');
				});
				submitter.classList.add('is-loading');

				var spinner = document.createElement('span');
				spinner.className = 'tmsmtp-button-spinner';
				spinner.setAttribute('aria-hidden', 'true');
				var status = document.createElement('span');
				status.className = 'screen-reader-text tmsmtp-button-status';
				status.textContent = processingText;
				submitter.appendChild(spinner);
				submitter.appendChild(status);

				window.requestAnimationFrame(function () {
					window.requestAnimationFrame(function () {
						window.HTMLFormElement.prototype.submit.call(form);
					});
				});
			});
		});

		window.addEventListener('pageshow', resetBusyForms);
	}

	function initializeDarkModeToggle() {
		var toggle = document.querySelector('.tmsmtp-dark-toggle-input');
		if (!toggle) {
			return;
		}

		var apply = function (enabled) {
			document.documentElement.classList.toggle('tmsmtp-admin-dark', enabled);
			toggle.checked = enabled;
		};

		try {
			apply((window.localStorage.getItem('tmsmtpAdminDarkMode') || window.localStorage.getItem('tmsAdminDarkMode')) === '1');
		} catch (error) {
			apply(false);
		}

		toggle.addEventListener('change', function () {
			apply(toggle.checked);
			try {
				window.localStorage.setItem('tmsmtpAdminDarkMode', toggle.checked ? '1' : '0');
				window.localStorage.removeItem('tmsAdminDarkMode');
			} catch (error) {
			}
		});
	}

	document.addEventListener('DOMContentLoaded', function () {
		observeExternalNotices();
		initializeProviderRows();
		initializeForms();
		initializeDarkModeToggle();
	});
}(window, document));
