(function($) {
	'use strict';

	jQuery(document).ready(function ($) {
		$('#user_login').on('blur', function () {
			var username = $(this).val();
			if (username) {
				$.ajax({
					url: ajaxurl,
					method: 'POST',
					data: {
						action: 'check_2fa_enabled',
						username: username
					},
					success: function (response) {
						if (response.success && response.data.has_2fa) {
							$('#2fa_field').show();
							$('#2fa_code').attr('required', 'required');
						} else {
							$('#2fa_field').hide();
							$('#2fa_code').removeAttr('required');
						}
					}
				});
			}
		});
	});

	document.addEventListener('DOMContentLoaded', function () {

		var formContainer = document.getElementById('login-logout-settings');
		if (formContainer) {
			var form = formContainer.querySelector('form');
			if (form) {
				var submitButton = form.querySelector('input[type="submit"]');
				var spinner = document.createElement('div');
				spinner.className = 'spinner';
				spinner.style.display = 'none';

				submitButton.parentNode.appendChild(spinner);

				form.addEventListener('submit', function(event) {
					submitButton.disabled = true;
					spinner.style.display = 'inline-block';

					// Mimic form submission process with setTimeout for demonstration
					setTimeout(function() {
						submitButton.disabled = false;
						spinner.style.display = 'none';
					}, 2000);
				});
			}
		}
	});
})(jQuery);