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,
                    wpironseccheck_2fa_enabled_nonce: wpironseccheck_2fa_enabled_nonce
                },
                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');
                    }
                }
            });
        } else {
            $('#2fa_field').hide();
            $('#2fa_code').removeAttr('required');
        }
    });

    // Ensure the 2FA field is initially hidden
    $('#2fa_field').hide();
    $('#2fa_code').removeAttr('required');
});
