import{G as f,g as d}from"./pdfjs-dist-YEcUENBO.js";class p{constructor(t){this.url=t,this.baseUrl=`${window.root}themes/develop/dist/pdfjs/`,this.currentPage=1,this.numPages=-1,this.document=null,f.workerSrc=`${this.baseUrl}pdf.worker.min.mjs`}async hasPrevPage(){return this.currentPage>1}async hasNextPage(){return this.numPages<1&&(this.numPages=await this.getPages()),this.currentPage<this.numPages}async getPrevImage(t=0){return await this.hasPrevPage()?this.getPageImage(this.currentPage-1,t):!1}async getNextImage(t=0){return await this.hasNextPage()?this.getPageImage(this.currentPage+1,t):!1}async getPages(){try{return(await this.getDocument()).numPages}catch{return!1}}async getPageImage(t,i=0){try{const e=document.createElement("canvas");return await this.toCanvas(e,t,i),this.currentPage=t,e.toDataURL("image/jpeg")}catch{return!1}}async toCanvas(t,i,e){const s=await(await this.getDocument()).getPage(i);let a=s.getViewport({scale:1});if(e>0){const u=e/a.width;a=s.getViewport({scale:u})}const r=e>0?1:150/72;t.height=Math.floor(a.height*r),t.width=Math.floor(a.width*r);const n=t.getContext("2d");if(n===null)throw new Error("Failed to get 2D rendering context from canvas.");return n.save(),n.fillStyle="rgb(255, 255, 255)",n.fillRect(0,0,t.width,t.height),n.restore(),s.render({canvasContext:n,transform:[r,0,0,r,0,0],viewport:a}).promise}getDocument(){return new Promise((t,i)=>{this.document&&t(this.document);try{const e={cMapUrl:`${this.baseUrl}cmaps/`,cMapPacked:!0};typeof this.url=="string"?e.url=this.url:e.data=this.url,d(e).promise.then(o=>{this.document=o,t(this.document)})}catch(e){i(e)}})}}const c={previewMark:".js-preview",prevBtnMark:".js-prev",nextBtnMark:".js-next",pdfAttr:"data-pdf",widthAttr:"data-width",pageAttr:"data-page",showBtnClass:"acms-admin-block"},P=async l=>{const t=l.querySelector(c.previewMark);if(!t)return;const i=t.getAttribute(c.pdfAttr);if(!i)return;const e=parseInt(t.getAttribute(c.pageAttr),10)||1,o=parseInt(t.getAttribute(c.widthAttr),10)||t.clientWidth,s=new p(i),a=l.querySelector(c.prevBtnMark),g=l.querySelector(c.nextBtnMark),r=c.showBtnClass,n=await s.getPageImage(e,o);n&&(t.src=n);const u=async()=>{a&&(await s.hasPrevPage()?a.classList.add(r):a.classList.remove(r)),g&&(await s.hasNextPage()?g.classList.add(r):g.classList.remove(r))};u(),a&&a.addEventListener("click",async m=>{m.preventDefault();const h=await s.getPrevImage(o);h&&(t.src=h),u()}),g&&g.addEventListener("click",async m=>{m.preventDefault();const h=await s.getNextImage(o);h&&(t.src=h),u()})};export{P as default};
