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