
/* ============================================================
 * PS_LOG — F12 telemetria FLOW (zawsze widoczna, bez debug gate)
 * Wstawiony 2026-05-14, plugin v4.4.1
 * ============================================================ */
window.PS_LOG = window.PS_LOG || function(stage, msg, data){
    try {
        var ts = new Date().toISOString().substring(11,23);
        var hdr = '%c[PS-FLOW ' + ts + ']%c ' + stage + (msg ? ' %c' + msg : '');
        var s1 = 'background:#1e40af;color:#fff;padding:2px 6px;border-radius:3px;font-weight:bold';
        var s2 = 'color:#1e40af;font-weight:bold;padding-left:4px';
        var s3 = 'color:#374151';
        if (data !== undefined) {
            console.groupCollapsed(hdr, s1, s2, s3);
            try { console.log('data:', data); } catch(_){}
            try { console.log('href:', location.pathname); } catch(_){}
            console.groupEnd();
        } else {
            console.log(hdr, s1, s2, s3);
        }
    } catch(_){}
};
/* Parcel Selector — Label Stage 4 payment polling + QR render.
 * Vars injected via wp_add_inline_script. */

		(function(){
			var qrUri = window.PARCSELECT_LABEL_PAY.qrUri;
			// NS-38 hotfix 2026-04-30: race-condition retry-poll (library w footer, inline IIFE w body).
			// Docelowy fix v4.3.8: server-side PNG z VPS.
			(function renderQR(){
				var canvas = document.getElementById('parcselect-qr-canvas');
				if (!canvas || !qrUri) return;
				if (canvas.querySelector('canvas, img')) return;
				if (typeof window.QRCode === 'undefined') {
					var attempts = 0;
					var iv = setInterval(function(){
						attempts++;
						if (typeof window.QRCode !== 'undefined') { clearInterval(iv); renderQR(); }
						else if (attempts > 50) {
							clearInterval(iv);
							canvas.innerHTML = '<div style="padding:80px 0;color:#dc2626;font-size:12px;text-align:center">QR library timeout (5s)</div>';
							if (window.console) console.error('[PS-LABEL Stage4] QRCode library timeout');
						}
					}, 100);
					return;
				}
				new QRCode(canvas, { text: qrUri, width: 248, height: 248, correctLevel: QRCode.CorrectLevel.M });
			})();
			// Polling check_payment every 10s.
			var statusEl = document.getElementById('parcselect-payment-text');
			var tries = 0;
			function poll(){
				tries++;
				window.PS_LOG && window.PS_LOG('POLL_TICK', 'check_payment #' + tries, {order_id: window.PARCSELECT_LABEL_PAY.orderId});
				if (tries > 360) { // ~1h limit
					if (statusEl) statusEl.textContent = 'Timeout — odśwież stronę lub sprawdź webhook.';
					return;
				}
				var fd = new FormData();
				fd.append('action', 'parcselect_label_check_payment');
				fd.append('nonce', window.PARCSELECT_LABEL_PAY.nonce);
				fd.append('order_id', window.PARCSELECT_LABEL_PAY.orderId);
				fetch(ajaxurl, {method:'POST', body:fd, credentials:'same-origin'})
					.then(function(r){return r.json();})
					.then(function(j){
						window.PS_LOG && window.PS_LOG('POLL_RESPONSE', j && j.data && j.data.reload ? 'PAID - reload' : 'pending', j);
						if (j && j.success && j.data && j.data.reload) {
							if (statusEl) statusEl.textContent = '✓ Płatność potwierdzona — odświeżam stronę…';
							setTimeout(function(){ window.location.reload(); }, 800);
							return;
						}
						setTimeout(poll, 10000);
					})
					.catch(function(){ setTimeout(poll, 15000); });
			}
			setTimeout(poll, 5000); // first poll after 5s
		})();