
/* ============================================================
 * 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 pick-size buttons. */

		(function(){
			var buttons = document.querySelectorAll('.parcselect-pick-size');
			var errBox  = document.getElementById('parcselect-pick-error');
			function showErr(msg){ if(errBox){errBox.textContent = msg; errBox.style.display='block';} }
			buttons.forEach(function(btn){
				btn.addEventListener('click', function(){
					var size = btn.getAttribute('data-size');
					window.PS_LOG && window.PS_LOG('SIZE_CLICK', 'user wybral rozmiar=' + size, {size: size, order_id: window.PARCSELECT_PICK_SIZE.orderId});
					// v4.1.7: SELECTED GREEN state — NIE disappear
					buttons.forEach(function(b){
						b.disabled = true;
						if (b !== btn) {
							b.style.opacity = '0.35';
							b.style.filter = 'grayscale(1)';
						}
					});
					btn.classList.add('selected');
					btn.style.borderColor = '#10b981';
					btn.style.background = 'linear-gradient(135deg,#ecfdf5 0%,#d1fae5 100%)';
					btn.style.boxShadow = '0 4px 12px rgba(16,185,129,.25)';
					btn.innerHTML += '<div style="margin-top:8px;font-size:11px;color:#065f46;font-weight:700">&#10003; Wybrane — tworzenie sesji…</div>';
					var fd = new FormData();
					fd.append('action', 'parcselect_label_create_session');
					fd.append('nonce',  window.PARCSELECT_PICK_SIZE.nonce);
					fd.append('order_id', window.PARCSELECT_PICK_SIZE.orderId);
					fd.append('size', size);
					fd.append('weight', '1.0');
					(function(){ window.PS_LOG && window.PS_LOG('SESSION_CREATE_REQUEST', 'POST parcselect_label_create_session', {size: size, weight: '1.0'}); })();
					fetch(ajaxurl, {method:'POST', body:fd, credentials:'same-origin'})
						.then(function(r){
							// DEV-5 2026-05-16: serwer moze oddac HTML (strona bledu /
							// timeout / redirect na login) zamiast JSON -> r.json()
							// rzucalo "Unexpected token '<'". Czytamy tekst i parsujemy
							// recznie; przy nie-JSON rzucamy czytelny komunikat.
							return r.text().then(function(t){
								try { return JSON.parse(t); }
								catch(_e){
									window.PS_LOG && window.PS_LOG('SESSION_CREATE_BADJSON', 'HTTP ' + r.status + ' nie-JSON', {status: r.status, body: String(t).slice(0,200)});
									throw new Error('serwer zwrocil HTTP ' + r.status + ' (nie JSON) - sprobuj ponownie za chwile');
								}
							});
						})
						.then(function(j){
							window.PS_LOG && window.PS_LOG('SESSION_CREATE_RESPONSE', j && j.success ? 'success - reload' : 'fail', j);
							if (j && j.success) {
								window.location.reload();
							} else {
								var msg = (j && j.data && j.data.message) ? j.data.message : 'unknown_error';
								showErr('Błąd: ' + msg + ' — odśwież stronę i spróbuj ponownie.');
								buttons.forEach(function(b){ b.disabled = false; });
							}
						})
						.catch(function(e){
							showErr('Błąd sieci: ' + (e && e.message ? e.message : 'unknown'));
							buttons.forEach(function(b){ b.disabled = false; });
						});
				});
			});
		})();