(()=>{"use strict";const t={MODE_TERMINATOR:0,MODE_NUMERIC:1,MODE_ALPHANUMERIC:2,MODE_OCTET:4,isMode:t=>"124".includes(t+""),ECCLEVEL_M:0,ECCLEVEL_L:1,ECCLEVEL_H:2,ECCLEVEL_Q:3,isEccl:t=>t>-1&&t<4,bitsFieldDataQuantity:(e,s)=>{switch(s){case t.MODE_NUMERIC:return e<10?10:e<27?12:14;case t.MODE_ALPHANUMERIC:return e<10?9:e<27?11:13;case t.MODE_OCTET:return e<10?8:16}return 0},ALPHANUMERIC_MAP:"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:".split("").reduce(((t,e,s)=>(t[e]=s,t)),{}),GF256:[],GF256_INV:[-1],GF256_GENPOLY:{},encodeBCH:(t,e,s,r)=>{let i=t<=0;--t)i>>r+t&1&&(i^=s<(t+e)%2==0,(t,e)=>t%2==0,(t,e)=>e%3==0,(t,e)=>(t+e)%3==0,(t,e)=>((t/2|0)+(e/3|0))%2==0,(t,e)=>t*e%2+t*e%3==0,(t,e)=>(t*e%2+t*e%3)%2==0,(t,e)=>((t+e)%2+t*e%3)%2==0],PENALTY:{CONSECUTIVE:3,TWOBYTWO:3,FINDERLIKE:40,DENSITY:10},IMAGE:["PNG","SVG","HTML","NONE"],modsize:4,margin:4};for(let e=0,s=1;e<255;++e)t.GF256.push(s),t.GF256_INV[s]=e,s=2*s^(s>=128?285:0);for(let e=0,s=[];e<30;++e){const r=[];for(let i=0;i<=e;++i){const o=i{const s=(t,e)=>({name:"QRoptionsError",setting:t,subcode:e}),r=t=>t!==(-1&t);try{if((t=>{try{if("string"!=typeof t)throw s("text","1");if(0===t.length)throw s("text","2")}catch(t){if("QRoptionsError"===t.name)throw t}})(e.text),((r,i)=>{try{const o=/^[A-Z0-9 $%*+\-./:]*$/,n=/^\d*$/;if(r!==t.MODE_NUMERIC&&-1!==r||i.replace(n,""))if(r!==t.MODE_ALPHANUMERIC&&-1!==r||i.replace(o,"")){if(r!==t.MODE_OCTET&&-1!==r)throw s("mode","1");r=t.MODE_OCTET}else r=t.MODE_ALPHANUMERIC;else r=t.MODE_NUMERIC}catch(t){if("QRoptionsError"===t.name)throw t}finally{e.mode=r}})(e.mode,e.text),e.textToData(),!e.data.length)throw s("text","3");if(e.eccl<-1||e.eccl>3||r(e.eccl))throw s("eccl","1");if(((i,o)=>{const n=e.mode,l=s=>(e.version=s,e.data.length<=(()=>{const s=e.bitsData-4-e.bitsFieldDataQty;switch(n){case t.MODE_NUMERIC:return 3*(s/10|0)+(s%10<4?0:s%10<7?1:2);case t.MODE_ALPHANUMERIC:return 2*(s/11|0)+(s%11<6?0:1);case t.MODE_OCTET:return s/8|0}})()),a=(t,s,r=0)=>{const i=[2,3,0,1];if(!s)return l(t);for(let s=r;s<4;++s)if(e.eccl=i[s],l(t))return!0;return!1};try{if(-1===i){for(o&&(e.eccl=2),i=1;i<41&&!l(i);++i);if(i>40&&(!o||!a(40,o,1)))throw s("version","1")}else{if(i<1||i>40||r(i))throw s("version","2");if(!a(i,o))throw s("version","3")}}catch(t){if("QRoptionsError"===t.name)throw t}})(e.version,e.eccl<0),-1!==e.mask&&(e.mask<0||e.mask>8||r(e.mask)))throw s("mask","1");if(!t.IMAGE.includes(e.image))throw s("image","1");if(-1===e.modsize)e.modsize=t.modsize;else if(e.modsize<1||r(e.modsize))throw s("modsize","1");if(-1===e.margin)e.margin=t.margin;else if(e.margin<0||r(e.margin))throw s("margin","1")}catch(t){e.error=t.setting,e.errorSubcode=t.subcode}};class r{constructor(t,e,s){for(const t in e)Object.defineProperty(this,t,{enumerable:!0,value:e[t]});for(const t in s)Object.defineProperty(this,t,{writable:!0,value:s[t]});Object.defineProperty(this,"_image",{writable:!0,value:""}),Object.defineProperty(this,"result",{writable:!0,value:""}),this.image=t}set image(e){e=e.trim().toUpperCase(),this._image=e,this.result="","image"===this.error&&this.clearError(),t.IMAGE.includes(e)||(this.error="image",this.errorSubcode="3"),this.error||(()=>{({NONE:()=>{this.result=""},PNG:()=>{const t=this.matrix,e=this.matrix.length,s=this.modsize,r=this.margin,i=s*(e+2*r),o=document.createElement("canvas");let n;if(o.width=o.height=i,n=o.getContext("2d"),!n)return this.error="image",void(this.errorSubcode="2");n.fillStyle="#fff",n.fillRect(0,0,i,i),n.fillStyle="#000";for(let i=0;i{const t=document.createElementNS("http://www.w3.org/2000/svg","svg"),e=this.matrix,s=this.matrix.length,r=this.modsize,i=this.margin,o=r*(s+2*i),n=["",''],l=' class= "fg" width="'+r+'" height="'+r+'"/>';t.setAttribute("viewBox","0 0 "+o+" "+o),t.setAttribute("style","shape-rendering:crispEdges");let a=i*r;for(let t=0;t{const t=document.createElement("div"),e=this.matrix,s=this.matrix.length,r=this.modsize,i=[``];for(let t=0;t");for(let o=0;o');i.push("")}t.className="qrcode",t.innerHTML=i.join("")+"
",this.result=t}})[this.image]()})()}get image(){return this._image}download(t="",e=this.image){let s="",r="";if(this.image=e,this.result){switch(this.image){case"PNG":t||(t="qrcode.png"),s=this.result.toDataURL();break;case"SVG":t||(t="qrcode.svg"),s=`${this.result.innerHTML}`,s=encodeURIComponent(s),r="data:image/svg+xml;charset=utf-8";break;case"HTML":t||(t="qrcode.html"),s=encodeURIComponent(this.result.innerHTML),r="data:text/html;charset=utf-8"}((t,e,s)=>{const r=document.createElement("a");e&&(e+=","),r.setAttribute("href",`${e}${t}`),r.setAttribute("download",s),r.click()})(s,r,t)}}clearError(){this.error="",this.errorSubcode=""}}class i{constructor(t="",e={}){let{mode:s,eccl:r,version:i,mask:o,image:n,modsize:l,margin:a}={mode:-1,eccl:-1,version:-1,mask:-1,image:"PNG",modsize:-1,margin:-1,...e};this.text=t,this.mode=s,this.eccl=r,this._version=i,this.mask=o,this.image=n.trim().toUpperCase(),this.modsize=l,this.margin=a,this.error="",this.errorSubcode="",this.bitsData=0,this.qtyBlocks=0,this.bytesCorrectionPerBlock=0,this.genpoly=[],this.bitsFieldDataQty=0,this.positionAlignmentPatterns=[],this.data=[],this.codewordsData=[],this.codewordsQR=[],this.matrix=[]}set version(e){const s=this.mode,r=this.eccl;this._version=e,t.isMode(s)&&t.isEccl(r)&&e>0&&e<41&&this.fixedInfoVersion(e,s,r)}get version(){return this._version}get needsVerInfo(){return this.version>6}fixedInfoVersion(s,r,i){this.qtyBlocks=e[s][1][i],this.bytesCorrectionPerBlock=e[s][0][i],this.genpoly=t.GF256_GENPOLY[this.bytesCorrectionPerBlock],this.bitsFieldDataQty=t.bitsFieldDataQuantity(s,r),this.positionAlignmentPatterns=e[s][2],this.bitsData=(-8&(()=>{let t=16*s*s+128*s+64;const e=this.positionAlignmentPatterns.length;return e&&(t-=25*e*e-10*e-55),this.needsVerInfo&&(t-=36),t})())-8*this.bytesCorrectionPerBlock*this.qtyBlocks}textToData(){const e=this.text,s=this.data;if(this.mode!==t.MODE_NUMERIC&&this.mode!==t.MODE_ALPHANUMERIC)for(const t of e){const e=t.codePointAt(0);if(e<128)s.push(e);else{if(e>2097151){s.length=0;break}{const t=e<2048?1:e<65536?2:3;s.push([192,224,240][t-1]|e>>6*t);for(let r=t;r>0;)s.push(128|e>>6*--r&63)}}}else this.data=e.split("")}dataToCodewords(){const e=this.data,s=e.length,r=this.mode,i=this.bitsData>>3,o=this.codewordsData;let n=0,l=8;const a=(t,e)=>{for(;e>0;)t&=(1<>>(e-=l),o.push(n),n=0,l=8)};switch(a(r,4),a(s,this.bitsFieldDataQty),r){case t.MODE_NUMERIC:for(let t=2;ta(t,8)))}for((o.length{const r=[...e].concat(Array(s.length).fill(0));for(let i=0;i=0)for(let o=0;o{const s=[],r=t/e|0,i=e-t%e;for(let t=0,o=0;t{for(let i=0;i>o&1},f=t=>{const e=[0,1,2,3,4,5,7,8,h-7,h-6,h-5,h-4,h-3,h-2,h-1],s=[h-1,h-2,h-3,h-4,h-5,h-6,h-7,h-8,7,5,4,3,2,1,0];for(let r=0;r<15;++r)c[8][s[r]]=c[e[r]][8]=t>>r&1};for(let t=0;t>e++&1;for(let t=h-1,e=0,r=-1;t>-1;t-=2){6===t&&--t;for(let o=0;ot-2;r--)if(null===c[n][r]){const t=a[e>>3]>>(7&~e)&1;c[n][r]=t^i(n,r),s&&(d[n][r]=t),++e}}r=-r}if(s){const e=e=>{const s=t.MASKFUNCS[e];let r=this.makeCodewordFormat(e);f(r),d.forEach(((t,e)=>{t.forEach(((t,r)=>{null!==t&&(c[e][r]=t^s(e,r))}))}))},s=Array(t.MASKFUNCS.length).fill(0).map(((t,s)=>(s&&e(s),this.maskTest(c))));r=s.reduce(((t,e,s)=>(e{let s=0;for(let r=0;r=5&&(s+=t.PENALTY.CONSECUTIVE+(e[r]-5));for(let r=5;r=4||e[r+1]>=4)&&(s+=t.PENALTY.FINDERLIKE);return s};for(let l=0;l{const r=new i(t,e);return s(r),r.error||(r.dataToCodewords(),r.makeCodewordsQR(),r.makeMatrix()),r.report()}})();