/**
 * WalletUp Click Tracking
 * Tracks clicks on payment links/buttons/QR codes via sendBeacon to admin-ajax.php.
 * This avoids Cloudflare challenges since the user is already on the page.
 */
(function () {
    if (!window.walletUpTrack) {
        console.warn('[WalletUp] walletUpTrack config not found');
        return;
    }
    console.log('[WalletUp] Click tracking initialized', window.walletUpTrack.ajaxUrl);
    var tracked = {};
    document.addEventListener('click', function (e) {
        var el = e.target.closest('[data-wallet-up-method]');
        if (!el) {
            // Also try walletup-qr-link and walletup-button-link classes
            el = e.target.closest('.walletup-qr-link, .walletup-button-link, .walletup-payment-link');
        }
        if (!el) return;
        var method = el.getAttribute('data-wallet-up-method') || el.getAttribute('data-walletup-link') || 'unknown';
        var postId = el.getAttribute('data-wallet-up-post') || '0';
        var key = method + '_' + postId;
        if (tracked[key]) {
            console.log('[WalletUp] Click already tracked for', key);
            return;
        }
        tracked[key] = true;
        var d = new FormData();
        d.append('action', 'wallet_up_track_click');
        d.append('nonce', window.walletUpTrack.nonce);
        d.append('method', method);
        d.append('post_id', postId);
        var sent = navigator.sendBeacon(window.walletUpTrack.ajaxUrl, d);
        console.log('[WalletUp] Click tracked:', method, 'post:', postId, 'sent:', sent);
    }, true);
})();
