import{ApplicationContext as t,StringUtils as r}from"@sankhyalabs/core";class n{constructor(){this.appletImpressao=new i,t.getContextValue("__SNK__APPLICATION__").getDataFetcher().then((t=>this.DataFetcher=t))}static getInstance(){return n.instance||(n.instance=new n),n.instance}async processPendingPrinting(t){await this.findPendingPrints(t)}async findPendingPrints(t){const r={transactionIds:[{transactionId:{$:t}}]};Promise.resolve(this.DataFetcher).then((async t=>{for(t=await Promise.resolve(this.DataFetcher);!t||!t.application;)t=await Promise.resolve(this.DataFetcher);const i=await t.application.callServiceBroker(n.SERVICE_FIND_PENDING_PRINTS,JSON.stringify(r)),e=this.parsePrintData(i),s=await this.processDirectPrint(e);s.length&&this.openSnkPrintSelector(Object.assign(Object.assign({},e),{pendingPrinters:s}))}))}getLocalPrinters(){const t=this.appletImpressao.findLocalPrinters(),r=[];for(const i of t)r.push({nome:i,printerUri:n.LOCAL_SERVER_URI+i,isLocal:!0});return r}parsePrintData(t){const r=t.pendindPrintJobData;if(!r)return;let{printServers:{printServer:n},pendingPrinters:{pendingPrinter:i}}=r;n=n?Array.isArray(n)?n:[n]:[],i=i?Array.isArray(i)?i:[i]:[];const e=n.map((t=>{const r=Array.isArray(t.printerLocation)?t.printerLocation:[t.printerLocation];return{printServerUri:t.printServerUri,printerList:r.map(this.normalize)}})),s=i.map(this.normalize);return{transactionId:r.transactionId.$,printServers:e,pendingPrinters:s,printServerActive:"true"===r.printServers.printServerActive}}normalize(t){const r={};return Object.keys(t).forEach((i=>{r[i]=n.ENCODED_PROPERTIES.includes(i)?window.atob(t[i].$):t[i].$})),r}processLocalPrinting(t){const r=t.map((t=>t.transactionId));this.appletImpressao.doLocalPrinting(r.join())}async openSnkPrintSelector(t){let n=document.querySelector("snk-print-selector");n||(n=document.createElement("snk-print-selector"),n.setAttribute("id",r.generateUUID()),window.document.body.appendChild(n));const{selectedPrinter:i,saveSubstitute:e}=await n.openPrintSelector(t);if(!i)return;const s={transactionId:t.transactionId,pendingPrinters:t.pendingPrinters,printerUri:i.printerUri,saveSubstitutePrinters:e};await this.saveSubstitutePrinter(s)}async saveSubstitutePrinter(t){const{transactionId:r,pendingPrinters:i,printerUri:e,saveSubstitutePrinters:s=!1}=t,a={substitutePrintersRequest:{substitutePrinters:{pendingPrinter:i.map((t=>({printerUri:{$:t.printerUri||e},originalPrinterName:{$:t.originalPrinterName},printJobCount:{$:t.printJobCount},docType:{$:t.docType},docTypeDescription:{$:t.docTypeDescription}})))},saveSubstitutePrinters:{$:s.toString()},transactionId:{$:r}}};Promise.resolve(this.DataFetcher).then((async t=>{await t.application.callServiceBroker(n.SERVICE_SAVE_SUBSTITUTE_PRINTER,JSON.stringify(a))}))}async processDirectPrint(t){if(!this.appletImpressao.checkWebConnection())return[];const r=this.getLocalPrinters();if(!r.length)return[];const i=[],e=t.pendingPrinters.filter((t=>{const e=this.getDefaultPrinterName(t.originalPrinterName);return!(r.find((t=>t.nome===e))||this.isPrinterNameAFile(e))||(i.push(Object.assign(Object.assign({},t),{printerUri:n.LOCAL_SERVER_URI+e})),!1)}));if(!i.length)return e;const s={transactionId:t.transactionId,pendingPrinters:i};return await this.saveSubstitutePrinter(s),e}getDefaultPrinterName(t){const r=this.appletImpressao.getDefaultPrinter(),n=/padr[aã]o/gi.test(t);return r&&n?r:t}isPrinterNameAFile(t){return new RegExp("^(w:)?[/\\ws]+?.w{3}$","mis").test(t)}}n.SERVICE_FIND_PENDING_PRINTS="mge@PrintServiceSP.findPendingPrinters",n.SERVICE_SAVE_SUBSTITUTE_PRINTER="mge@PrintServiceSP.saveSubstitutePrinter",n.LOCAL_SERVER_URI="LOCAL:0/",n.ENCODED_PROPERTIES=["printerName","printerUri"];class i{constructor(){this.appletImpressao=document.getElementById("centralNotaAppletImpressao")}checkWebConnection(){return!!this.appletImpressao&&(!!this.appletImpressao.testRunningWC&&!!this.appletImpressao.testRunningWC())}findLocalPrinters(){if(!this.appletImpressao)return[];const t=this.appletImpressao.getLocalPrinters();return t?t.split(","):[]}doLocalPrinting(t){this.appletImpressao&&this.appletImpressao.doLocalPrinting(t)}getDefaultPrinter(){return this.appletImpressao?this.appletImpressao.getPrintDefault():""}}export{n as P}