import{defineComponent as _,computed as Z,shallowRef as p,watchEffect as y,watch as k,createVNode as h,Fragment as q}from"vue";import{qrProps as D}from"./interface.mjs";import m from"./qrcodegen.mjs";const F={L:m.QrCode.Ecc.LOW,M:m.QrCode.Ecc.MEDIUM,Q:m.QrCode.Ecc.QUARTILE,H:m.QrCode.Ecc.HIGH},A=128,z="L",P="#FFFFFF",T="#000000",U=!1,B=4,j=0,X=.1;function Q(n,e=0){const c=[];return n.forEach((l,o)=>{let t=null;l.forEach((i,r)=>{if(!i&&t!==null){c.push(`M${t+e} ${o+e}h${r-t}v1H${t+e}z`),t=null;return}if(r===l.length-1){if(!i)return;t===null?c.push(`M${r+e},${o+e} h1v1H${r+e}z`):c.push(`M${t+e},${o+e} h${r+1-t}v1H${t+e}z`);return}i&&t===null&&(t=r)})}),c.join("")}function H(n,e){return n.slice().map((c,l)=>l<e.y||l>=e.y+e.h?c:c.map((o,t)=>t<e.x||t>=e.x+e.w?o:!1))}function G(n,e,c,l){if(l==null)return null;const o=n.length+c*2,t=Math.floor(e*X),i=o/e,r=(l.width||t)*i,u=(l.height||t)*i,a=l.x==null?n.length/2-r/2:l.x*i,g=l.y==null?n.length/2-u/2:l.y*i;let C=null;if(l.excavate){const s=Math.floor(a),L=Math.floor(g),R=Math.ceil(r+a-s),S=Math.ceil(u+g-L);C={x:s,y:L,w:R,h:S}}return{x:a,y:g,h:u,w:r,excavation:C}}function b(n,e){return e!=null?Math.floor(e):n?B:j}const Y=(function(){try{new Path2D().addPath(new Path2D)}catch{return!1}return!0})(),le=_({name:"QRCodeCanvas",inheritAttrs:!1,props:{...D(),level:String,bgColor:String,fgColor:String,marginSize:Number},setup(n,{attrs:e,expose:c}){const l=Z(()=>n.imageSettings?.src),o=p(null),t=p(null),i=p(!1);return c({toDataURL:(r,u)=>o.value?.toDataURL(r,u)}),y(()=>{const{value:r,size:u=A,level:a=z,bgColor:g=P,fgColor:C=T,includeMargin:s=U,marginSize:L,imageSettings:R}=n;if(o.value!=null){const S=o.value,f=S.getContext("2d");if(!f)return;let E=m.QrCode.encodeText(r,F[a]).getModules();const d=b(s,L),w=E.length+d*2,v=G(E,u,d,R),M=t.value,I=i.value&&v!=null&&M!==null&&M.complete&&M.naturalHeight!==0&&M.naturalWidth!==0;I&&v.excavation!=null&&(E=H(E,v.excavation));const $=window.devicePixelRatio||1;S.height=S.width=u*$;const x=u/w*$;f.scale(x,x),f.fillStyle=g,f.fillRect(0,0,w,w),f.fillStyle=C,Y?f.fill(new Path2D(Q(E,d))):E.forEach((N,O)=>{N.forEach((V,W)=>{V&&f.fillRect(W+d,O+d,1,1)})}),I&&f.drawImage(M,v.x+d,v.y+d,v.w,v.h)}},{flush:"post"}),k(l,()=>{i.value=!1}),()=>{const r=n.size??A,u={height:`${r}px`,width:`${r}px`};let a=null;return l.value!=null&&(a=h("img",{src:l.value,key:l.value,style:{display:"none"},onLoad:()=>{i.value=!0},ref:t},null)),h(q,null,[h("canvas",{...e,style:[u,e.style],ref:o},null),a])}}}),te=_({name:"QRCodeSVG",inheritAttrs:!1,props:{...D(),color:String,level:String,bgColor:String,fgColor:String,marginSize:Number,title:String},setup(n){let e=null,c=null,l=null,o=null,t=null,i=null;return y(()=>{const{value:r,size:u=A,level:a=z,includeMargin:g=U,marginSize:C,imageSettings:s}=n;e=m.QrCode.encodeText(r,F[a]).getModules(),c=b(g,C),l=e.length+c*2,o=G(e,u,c,s),s!=null&&o!=null&&(o.excavation!=null&&(e=H(e,o.excavation)),i=h("image",{"xlink:href":s.src,height:o.h,width:o.w,x:o.x+c,y:o.y+c,preserveAspectRatio:"none"},null)),t=Q(e,c)}),()=>{const r=n.bgColor&&P,u=n.fgColor&&T;return h("svg",{height:n.size,width:n.size,viewBox:`0 0 ${l} ${l}`},[!!n.title&&h("title",null,[n.title]),h("path",{fill:r,d:`M0,0 h${l}v${l}H0z`,"shape-rendering":"crispEdges"},null),h("path",{fill:u,d:t,"shape-rendering":"crispEdges"},null),i])}}});export{le as QRCodeCanvas,te as QRCodeSVG};
