/**
 * Live Traffic Beacon - JavaScript-based human detection
 *
 * This script marks the visitor as human by making an AJAX call.
 * Used for non-cached visitors to improve human detection accuracy.
 *
 * @package VMPFence
 * @since 2.1.0
 */

(function($) {
    'use strict';

    // Only run once per page load
    if (window.vmpfenceBeaconFired) {
        return;
    }
    window.vmpfenceBeaconFired = true;

    // Fire beacon on DOM ready
    $(document).ready(function() {
        // Check if beacon data is available
        if (typeof vmpfenceBeacon === 'undefined') {
            return;
        }

        // Make AJAX call to mark visitor as human
        $.ajax({
            url: vmpfenceBeacon.ajaxUrl,
            type: 'POST',
            data: {
                action: 'vmpfence_mark_human_visitor',
                nonce: vmpfenceBeacon.nonce
            },
            dataType: 'json',
            timeout: 5000, // 5 second timeout
            success: function(response) {
                if (response.success) {
                    // Successfully marked as human
                    // Can add debug logging here if needed
                    // if (window.console && console.log) {
                    //     console.log('VMPFence: Visitor marked as human');
                    // }
                }
            },
            error: function(xhr, status, error) {
                // Silently fail - don't disrupt user experience
                if (window.console && console.warn) {
                    console.warn('VMPFence beacon failed:', status);
                }
            }
        });
    });

})(jQuery);
