jQuery(document).ready(function(e){if(backendMetaboxHandlerData=fiksef_metaboxHandlerData,"undefined"==typeof backendMetaboxHandlerData||"undefined"==typeof KSEF_Common)return void console.error("backendMetaboxHandlerData or KSEF_Common is not defined. Document sending script will not run.");const n=e(".fi-init-ksef"),t=e("#wpdesk_ksef .inside");if(0===n.length||0===t.length)return;const a=backendMetaboxHandlerData.documentId||n.first().data("document-id");if(!a)return void console.error("Document ID not found for KSeF operations.");const o=backendMetaboxHandlerData.ajaxUrl,s=backendMetaboxHandlerData.ajaxAction;function i(){t.empty()}function c(e){t.append(e)}function r(e,n=!0){n&&i(),c(e)}t.on("click",".fi-init-ksef",async function(n){n.preventDefault(),e(this).remove(),await async function(){i(),c('<span style="float:none" class="spinner is-active"></span>');const e=await KSEF_Common.sendInitialKsefRequest(o,s,a,backendMetaboxHandlerData.nonce);r(e.message),"error"!==e.status&&"already_archived"!==e.status&&("success"===e.status||"awaiting"===e.status)&&e.documentReferenceNumber&&e.sessionReferenceNumber&&"max_attempts"===(await KSEF_Common.pollKsefDocumentStatus(o,"fiksef_check_document_status",{document:e.documentReferenceNumber,session:e.sessionReferenceNumber},a,backendMetaboxHandlerData.pollNonce,{onAttempt:function(e){r(e.message)}})).status&&c("<br/>Nie udało się potwierdzić statusu w KSeF.")}()}),setInterval(function(){e("body").hasClass("invoice-changed")&&e(".ksef-sending-warning").show()},500)});