ray-template/js/renderer-main-C5SCc8wx.js

1 line
28 KiB
JavaScript

import{r as e}from"./qrcode-DK1OzDFl.js";import{t}from"./preload-helper-B5zr4nmU.js";var n=Object.freeze({L:1,M:0,Q:3,H:2});function r(){if(typeof document>`u`&&typeof OffscreenCanvas>`u`)throw Error(`Canvas rendering is only available in browser environments`)}function i(e=`png`){return e.startsWith(`image/`)?e:e===`jpg`||e===`jpeg`?`image/jpeg`:e===`webp`?`image/webp`:e===`gif`?`image/gif`:`image/png`}function a(e){let[,t=``]=e.split(`,`);if(typeof atob!=`function`)throw Error(`Base64 decoding is not available in this environment`);let n=atob(t),r=new Uint8Array(n.length);for(let e=0;e<n.length;e++)r[e]=n.charCodeAt(e);return r}function o(e){let t=``,n=32768;for(let r=0;r<e.length;r+=n)t+=String.fromCharCode(...e.subarray(r,r+n));return btoa(t)}async function s(e){let t=new Uint8Array(await e.arrayBuffer());return`data:${e.type};base64,${o(t)}`}var c=class{constructor(e,t){if(r(),typeof document>`u`){let n=new OffscreenCanvas(e,t);return n.toDataURL=async(e=`png`)=>s(await n.convertToBlob({type:i(e)})),n.toBuffer=async(e=`png`)=>{let t=await n.convertToBlob({type:i(e)});return new Uint8Array(await t.arrayBuffer())},n}let n=document.createElement(`canvas`);return n.width=e,n.height=t,n.toBuffer=(e=`png`)=>a(n.toDataURL(i(e))),n}};function l(e){return typeof HTMLCanvasElement<`u`&&e instanceof HTMLCanvasElement||typeof OffscreenCanvas<`u`&&e instanceof OffscreenCanvas}function u(e,t){for(var r=1,i=f(e),a=0,o=w.length;a<o;a++){var s=0;switch(t){case n.L:s=w[a][0];break;case n.M:s=w[a][1];break;case n.Q:s=w[a][2];break;case n.H:s=w[a][3]}if(i<=s)break;r++}if(r>w.length)throw Error(`Too long data`);return r}function d(e){if(typeof TextEncoder<`u`)return Array.from(new TextEncoder().encode(e));let t=unescape(encodeURIComponent(e)),n=[];for(let e=0;e<t.length;e++)n.push(t.charCodeAt(e));return n}function f(e){let t=d(e);return t.length+(t.length===e.length?0:3)}function p(e,t,r){let i=m(t),a=e-1,o={[n.L]:0,[n.M]:1,[n.Q]:2,[n.H]:3};return i<=w[a][o[r]]}function m(e){let t=d(e);return t.length+(t.length===e.length?0:3)}var h=class{constructor(e){this.mode=_.MODE_8BIT_BYTE,this.data=e,this.parsedData=this.parseData()}parseData(){let e=d(this.data);return e.length!==this.data.length&&e.unshift(239,187,191),e}getLength(){return this.parsedData.length}write(e){this.parsedData.forEach(t=>e.put(t,8))}},g=class e{constructor(e=-1,t=n.L){this.moduleCount=0,this.dataList=[],this.typeNumber=e,this.errorCorrectLevel=t,this.moduleCount=0,this.dataList=[]}addData(e){if(this.typeNumber<=0)this.typeNumber=u(e,this.errorCorrectLevel);else if(this.typeNumber>40)throw Error(`Invalid QR version: ${this.typeNumber}`);else if(!p(this.typeNumber,e,this.errorCorrectLevel))throw Error(`Data is too long for QR version: ${this.typeNumber}`);let t=new h(e);this.dataList.push(t),this.dataCache=void 0}isDark(e,t){if(e<0||this.moduleCount<=e||t<0||this.moduleCount<=t)throw Error(`${e},${t}`);return this.modules[e][t]}getModuleCount(){return this.moduleCount}make(){this.makeImpl(!1,this.getBestMaskPattern())}makeImpl(t,n){this.moduleCount=this.typeNumber*4+17,this.modules=Array(this.moduleCount);for(let e=0;e<this.moduleCount;e++){this.modules[e]=Array(this.moduleCount);for(let t=0;t<this.moduleCount;t++)this.modules[e][t]=null}this.setupPositionProbePattern(0,0),this.setupPositionProbePattern(this.moduleCount-7,0),this.setupPositionProbePattern(0,this.moduleCount-7),this.setupPositionAdjustPattern(),this.setupTimingPattern(),this.setupTypeInfo(t,n),this.typeNumber>=7&&this.setupTypeNumber(t),this.dataCache??=e.createData(this.typeNumber,this.errorCorrectLevel,this.dataList),this.mapData(this.dataCache,n)}setupPositionProbePattern(e,t){for(let n=-1;n<=7;n++)if(!(e+n<=-1||this.moduleCount<=e+n))for(let r=-1;r<=7;r++)t+r<=-1||this.moduleCount<=t+r||(0<=n&&n<=6&&(r==0||r==6)||0<=r&&r<=6&&(n==0||n==6)||2<=n&&n<=4&&2<=r&&r<=4?this.modules[e+n][t+r]=!0:this.modules[e+n][t+r]=!1)}getBestMaskPattern(){if(Number.isInteger(this.maskPattern)&&Object.values(v).includes(this.maskPattern))return this.maskPattern;let e=0,t=0;for(let n=0;n<8;n++){this.makeImpl(!0,n);let r=y.getLostPoint(this);(n==0||e>r)&&(e=r,t=n)}return t}setupTimingPattern(){for(let e=8;e<this.moduleCount-8;e++)this.modules[e][6]??(this.modules[e][6]=e%2==0);for(let e=8;e<this.moduleCount-8;e++)this.modules[6][e]??(this.modules[6][e]=e%2==0)}setupPositionAdjustPattern(){let e=y.getPatternPosition(this.typeNumber);for(let t=0;t<e.length;t++)for(let n=0;n<e.length;n++){let r=e[t],i=e[n];if(this.modules[r][i]==null)for(let e=-2;e<=2;e++)for(let t=-2;t<=2;t++)e==-2||e==2||t==-2||t==2||e==0&&t==0?this.modules[r+e][i+t]=!0:this.modules[r+e][i+t]=!1}}setupTypeNumber(e){let t=y.getBCHTypeNumber(this.typeNumber);for(var n=0;n<18;n++){var r=!e&&(t>>n&1)==1;this.modules[Math.floor(n/3)][n%3+this.moduleCount-8-3]=r}for(var n=0;n<18;n++){var r=!e&&(t>>n&1)==1;this.modules[n%3+this.moduleCount-8-3][Math.floor(n/3)]=r}}setupTypeInfo(e,t){let n=this.errorCorrectLevel<<3|t,r=y.getBCHTypeInfo(n);for(var i=0;i<15;i++){var a=!e&&(r>>i&1)==1;i<6?this.modules[i][8]=a:i<8?this.modules[i+1][8]=a:this.modules[this.moduleCount-15+i][8]=a}for(var i=0;i<15;i++){var a=!e&&(r>>i&1)==1;i<8?this.modules[8][this.moduleCount-i-1]=a:i<9?this.modules[8][15-i-1+1]=a:this.modules[8][15-i-1]=a}this.modules[this.moduleCount-8][8]=!e}mapData(e,t){let n=-1,r=this.moduleCount-1,i=7,a=0;for(let o=this.moduleCount-1;o>0;o-=2)for(o==6&&o--;;){for(let n=0;n<2;n++)if(this.modules[r][o-n]==null){let s=!1;a<e.length&&(s=(e[a]>>>i&1)==1),y.getMask(t,r,o-n)&&(s=!s),this.modules[r][o-n]=s,i--,i==-1&&(a++,i=7)}if(r+=n,r<0||this.moduleCount<=r){r-=n,n=-n;break}}}static createData(t,n,r){let i=S.getRSBlocks(t,n),a=new C;for(var o=0;o<r.length;o++){let e=r[o];a.put(e.mode,4),a.put(e.getLength(),y.getLengthInBits(e.mode,t)),e.write(a)}let s=0;for(var o=0;o<i.length;o++)s+=i[o].dataCount;if(a.getLengthInBits()>s*8)throw Error(`code length overflow. (${a.getLengthInBits()}>${s*8})`);for(a.getLengthInBits()+4<=s*8&&a.put(0,4);a.getLengthInBits()%8!=0;)a.putBit(!1);for(;!(a.getLengthInBits()>=s*8||(a.put(e.PAD0,8),a.getLengthInBits()>=s*8));)a.put(e.PAD1,8);return e.createBytes(a,i)}static createBytes(e,t){let n=0,r=0,i=0,a=Array(t.length),o=Array(t.length);for(var s=0;s<t.length;s++){let l=t[s].dataCount,u=t[s].totalCount-l;r=Math.max(r,l),i=Math.max(i,u),a[s]=Array(l);for(var c=0;c<a[s].length;c++)a[s][c]=255&e.buffer[c+n];n+=l;let d=y.getErrorCorrectPolynomial(u),f=new x(a[s],d.getLength()-1).mod(d);o[s]=Array(d.getLength()-1);for(var c=0;c<o[s].length;c++){let e=c+f.getLength()-o[s].length;o[s][c]=e>=0?f.get(e):0}}let l=0;for(var c=0;c<t.length;c++)l+=t[c].totalCount;let u=Array(l),d=0;for(var c=0;c<r;c++)for(var s=0;s<t.length;s++)c<a[s].length&&(u[d++]=a[s][c]);for(var c=0;c<i;c++)for(var s=0;s<t.length;s++)c<o[s].length&&(u[d++]=o[s][c]);return u}};g.PAD0=236,g.PAD1=17;var _={MODE_NUMBER:1,MODE_ALPHA_NUM:2,MODE_8BIT_BYTE:4,MODE_KANJI:8},v={PATTERN000:0,PATTERN001:1,PATTERN010:2,PATTERN011:3,PATTERN100:4,PATTERN101:5,PATTERN110:6,PATTERN111:7},y=class e{static _cache=new Map;static getBCHTypeInfo(t){let n=`typeInfo_${t}`;if(this._cache.has(n))return this._cache.get(n);let r=t<<10;for(;e.getBCHDigit(r)-e.getBCHDigit(e.G15)>=0;)r^=e.G15<<e.getBCHDigit(r)-e.getBCHDigit(e.G15);let i=(t<<10|r)^e.G15_MASK;return this._cache.set(n,i),i}static getBCHTypeNumber(t){let n=t<<12;for(;e.getBCHDigit(n)-e.getBCHDigit(e.G18)>=0;)n^=e.G18<<e.getBCHDigit(n)-e.getBCHDigit(e.G18);return t<<12|n}static getBCHDigit(e){let t=0;for(;e!=0;)t++,e>>>=1;return t}static getPatternPosition(t){return e.PATTERN_POSITION_TABLE[t-1]}static getMask(e,t,n){let r={[v.PATTERN000]:(e,t)=>(e+t)%2==0,[v.PATTERN001]:(e,t)=>e%2==0,[v.PATTERN010]:(e,t)=>t%3==0,[v.PATTERN011]:(e,t)=>(e+t)%3==0,[v.PATTERN100]:(e,t)=>(Math.floor(e/2)+Math.floor(t/3))%2==0,[v.PATTERN101]:(e,t)=>e*t%2+e*t%3==0,[v.PATTERN110]:(e,t)=>(e*t%2+e*t%3)%2==0,[v.PATTERN111]:(e,t)=>(e*t%3+(e+t)%2)%2==0}[e];if(!r)throw Error(`Invalid mask pattern: ${e}`);return r(t,n)}static getErrorCorrectPolynomial(e){let t=new x([1],0);for(let n=0;n<e;n++)t=t.multiply(new x([1,b.gexp(n)],0));return t}static getLengthInBits(e,t){if(1<=t&&t<10)switch(e){case _.MODE_NUMBER:return 10;case _.MODE_ALPHA_NUM:return 9;case _.MODE_8BIT_BYTE:return 8;case _.MODE_KANJI:return 8;default:throw Error(`mode:${e}`)}else if(t<27)switch(e){case _.MODE_NUMBER:return 12;case _.MODE_ALPHA_NUM:return 11;case _.MODE_8BIT_BYTE:return 16;case _.MODE_KANJI:return 10;default:throw Error(`mode:${e}`)}else if(t<41)switch(e){case _.MODE_NUMBER:return 14;case _.MODE_ALPHA_NUM:return 13;case _.MODE_8BIT_BYTE:return 16;case _.MODE_KANJI:return 12;default:throw Error(`mode:${e}`)}else throw Error(`type:${t}`)}static getLostPoint(e){let t=e.getModuleCount(),n=0,r=new Map,i=(t,n)=>{let i=`${t},${n}`;return r.has(i)||r.set(i,e.isDark(t,n)),r.get(i)};for(var a=0;a<t;a++)for(var o=0;o<t;o++){let e=0,r=i(a,o);for(let n=-1;n<=1;n++)if(!(a+n<0||t<=a+n))for(let s=-1;s<=1;s++)o+s<0||t<=o+s||n==0&&s==0||r==i(a+n,o+s)&&e++;e>5&&(n+=3+e-5)}for(var a=0;a<t-1;a++)for(var o=0;o<t-1;o++){let e=0;i(a,o)&&e++,i(a+1,o)&&e++,i(a,o+1)&&e++,i(a+1,o+1)&&e++,(e==0||e==4)&&(n+=3)}for(var a=0;a<t;a++)for(var o=0;o<t-6;o++)i(a,o)&&!i(a,o+1)&&i(a,o+2)&&i(a,o+3)&&i(a,o+4)&&!i(a,o+5)&&i(a,o+6)&&(n+=40);for(var o=0;o<t;o++)for(var a=0;a<t-6;a++)i(a,o)&&!i(a+1,o)&&i(a+2,o)&&i(a+3,o)&&i(a+4,o)&&!i(a+5,o)&&i(a+6,o)&&(n+=40);let s=0;for(var o=0;o<t;o++)for(var a=0;a<t;a++)i(a,o)&&s++;let c=Math.abs(100*s/t/t-50)/5;return n+=c*10,n}};y.PATTERN_POSITION_TABLE=[[],[6,18],[6,22],[6,26],[6,30],[6,34],[6,22,38],[6,24,42],[6,26,46],[6,28,50],[6,30,54],[6,32,58],[6,34,62],[6,26,46,66],[6,26,48,70],[6,26,50,74],[6,30,54,78],[6,30,56,82],[6,30,58,86],[6,34,62,90],[6,28,50,72,94],[6,26,50,74,98],[6,30,54,78,102],[6,28,54,80,106],[6,32,58,84,110],[6,30,58,86,114],[6,34,62,90,118],[6,26,50,74,98,122],[6,30,54,78,102,126],[6,26,52,78,104,130],[6,30,56,82,108,134],[6,34,60,86,112,138],[6,30,58,86,114,142],[6,34,62,90,118,146],[6,30,54,78,102,126,150],[6,24,50,76,102,128,154],[6,28,54,80,106,132,158],[6,32,58,84,110,136,162],[6,26,54,82,110,138,166],[6,30,58,86,114,142,170]],y.G15=1335,y.G18=7973,y.G15_MASK=21522;var b=class e{static glog(t){if(t<1)throw Error(`glog(${t})`);return e.LOG_TABLE[t]}static gexp(t){for(;t<0;)t+=255;for(;t>=256;)t-=255;return e.EXP_TABLE[t]}};b.EXP_TABLE=Array(256),b.LOG_TABLE=Array(256),b._constructor=(function(){for(var e=0;e<8;e++)b.EXP_TABLE[e]=1<<e;for(var e=8;e<256;e++)b.EXP_TABLE[e]=b.EXP_TABLE[e-4]^b.EXP_TABLE[e-5]^b.EXP_TABLE[e-6]^b.EXP_TABLE[e-8];for(var e=0;e<255;e++)b.LOG_TABLE[b.EXP_TABLE[e]]=e})();var x=class e{constructor(e,t){if(e.length==null)throw Error(`${e.length}/${t}`);let n=0;for(;n<e.length&&e[n]==0;)n++;this.num=Array(e.length-n+t);for(let t=0;t<e.length-n;t++)this.num[t]=e[t+n]}get(e){return this.num[e]}getLength(){return this.num.length}multiply(t){let n=Array(this.getLength()+t.getLength()-1);for(let e=0;e<this.getLength();e++)for(let r=0;r<t.getLength();r++)n[e+r]^=b.gexp(b.glog(this.get(e))+b.glog(t.get(r)));return new e(n,0)}mod(t){if(this.getLength()-t.getLength()<0)return this;let n=b.glog(this.get(0))-b.glog(t.get(0)),r=Array(this.getLength());for(var i=0;i<this.getLength();i++)r[i]=this.get(i);for(var i=0;i<t.getLength();i++)r[i]^=b.gexp(b.glog(t.get(i))+n);return new e(r,0).mod(t)}},S=class e{constructor(e,t){this.totalCount=e,this.dataCount=t}static getRSBlocks(t,n){let r=e.getRsBlockTable(t,n);if(r==null)throw Error(`bad rs block @ typeNumber:${t}/errorCorrectLevel:${n}`);let i=r.length/3,a=[];for(let t=0;t<i;t++){let n=r[t*3+0],i=r[t*3+1],o=r[t*3+2];for(let t=0;t<n;t++)a.push(new e(i,o))}return a}static getRsBlockTable(t,r){switch(r){case n.L:return e.RS_BLOCK_TABLE[(t-1)*4+0];case n.M:return e.RS_BLOCK_TABLE[(t-1)*4+1];case n.Q:return e.RS_BLOCK_TABLE[(t-1)*4+2];case n.H:return e.RS_BLOCK_TABLE[(t-1)*4+3];default:return}}};S.RS_BLOCK_TABLE=[[1,26,19],[1,26,16],[1,26,13],[1,26,9],[1,44,34],[1,44,28],[1,44,22],[1,44,16],[1,70,55],[1,70,44],[2,35,17],[2,35,13],[1,100,80],[2,50,32],[2,50,24],[4,25,9],[1,134,108],[2,67,43],[2,33,15,2,34,16],[2,33,11,2,34,12],[2,86,68],[4,43,27],[4,43,19],[4,43,15],[2,98,78],[4,49,31],[2,32,14,4,33,15],[4,39,13,1,40,14],[2,121,97],[2,60,38,2,61,39],[4,40,18,2,41,19],[4,40,14,2,41,15],[2,146,116],[3,58,36,2,59,37],[4,36,16,4,37,17],[4,36,12,4,37,13],[2,86,68,2,87,69],[4,69,43,1,70,44],[6,43,19,2,44,20],[6,43,15,2,44,16],[4,101,81],[1,80,50,4,81,51],[4,50,22,4,51,23],[3,36,12,8,37,13],[2,116,92,2,117,93],[6,58,36,2,59,37],[4,46,20,6,47,21],[7,42,14,4,43,15],[4,133,107],[8,59,37,1,60,38],[8,44,20,4,45,21],[12,33,11,4,34,12],[3,145,115,1,146,116],[4,64,40,5,65,41],[11,36,16,5,37,17],[11,36,12,5,37,13],[5,109,87,1,110,88],[5,65,41,5,66,42],[5,54,24,7,55,25],[11,36,12],[5,122,98,1,123,99],[7,73,45,3,74,46],[15,43,19,2,44,20],[3,45,15,13,46,16],[1,135,107,5,136,108],[10,74,46,1,75,47],[1,50,22,15,51,23],[2,42,14,17,43,15],[5,150,120,1,151,121],[9,69,43,4,70,44],[17,50,22,1,51,23],[2,42,14,19,43,15],[3,141,113,4,142,114],[3,70,44,11,71,45],[17,47,21,4,48,22],[9,39,13,16,40,14],[3,135,107,5,136,108],[3,67,41,13,68,42],[15,54,24,5,55,25],[15,43,15,10,44,16],[4,144,116,4,145,117],[17,68,42],[17,50,22,6,51,23],[19,46,16,6,47,17],[2,139,111,7,140,112],[17,74,46],[7,54,24,16,55,25],[34,37,13],[4,151,121,5,152,122],[4,75,47,14,76,48],[11,54,24,14,55,25],[16,45,15,14,46,16],[6,147,117,4,148,118],[6,73,45,14,74,46],[11,54,24,16,55,25],[30,46,16,2,47,17],[8,132,106,4,133,107],[8,75,47,13,76,48],[7,54,24,22,55,25],[22,45,15,13,46,16],[10,142,114,2,143,115],[19,74,46,4,75,47],[28,50,22,6,51,23],[33,46,16,4,47,17],[8,152,122,4,153,123],[22,73,45,3,74,46],[8,53,23,26,54,24],[12,45,15,28,46,16],[3,147,117,10,148,118],[3,73,45,23,74,46],[4,54,24,31,55,25],[11,45,15,31,46,16],[7,146,116,7,147,117],[21,73,45,7,74,46],[1,53,23,37,54,24],[19,45,15,26,46,16],[5,145,115,10,146,116],[19,75,47,10,76,48],[15,54,24,25,55,25],[23,45,15,25,46,16],[13,145,115,3,146,116],[2,74,46,29,75,47],[42,54,24,1,55,25],[23,45,15,28,46,16],[17,145,115],[10,74,46,23,75,47],[10,54,24,35,55,25],[19,45,15,35,46,16],[17,145,115,1,146,116],[14,74,46,21,75,47],[29,54,24,19,55,25],[11,45,15,46,46,16],[13,145,115,6,146,116],[14,74,46,23,75,47],[44,54,24,7,55,25],[59,46,16,1,47,17],[12,151,121,7,152,122],[12,75,47,26,76,48],[39,54,24,14,55,25],[22,45,15,41,46,16],[6,151,121,14,152,122],[6,75,47,34,76,48],[46,54,24,10,55,25],[2,45,15,64,46,16],[17,152,122,4,153,123],[29,74,46,14,75,47],[49,54,24,10,55,25],[24,45,15,46,46,16],[4,152,122,18,153,123],[13,74,46,32,75,47],[48,54,24,14,55,25],[42,45,15,32,46,16],[20,147,117,4,148,118],[40,75,47,7,76,48],[43,54,24,22,55,25],[10,45,15,67,46,16],[19,148,118,6,149,119],[18,75,47,31,76,48],[34,54,24,34,55,25],[20,45,15,61,46,16]];var C=class{constructor(){this.buffer=new Uint8Array(32),this.length=0,this.capacity=32}_expandCapacity(){let e=new Uint8Array(this.capacity*2);e.set(this.buffer),this.buffer=e,this.capacity*=2}get(e){let t=Math.floor(e/8);return(this.buffer[t]>>>7-e%8&1)==1}put(e,t){for(let n=0;n<t;n++)this.putBit((e>>>t-n-1&1)==1)}getLengthInBits(){return this.length}putBit(e){let t=Math.floor(this.length/8);t>=this.capacity&&this._expandCapacity(),e&&(this.buffer[t]|=128>>>this.length%8),this.length++}},w=[[17,14,11,7],[32,26,20,14],[53,42,32,24],[78,62,46,34],[106,84,60,44],[134,106,74,58],[154,122,86,64],[192,152,108,84],[230,180,130,98],[271,213,151,119],[321,251,177,137],[367,287,203,155],[425,331,241,177],[458,362,258,194],[520,412,292,220],[586,450,322,250],[644,504,364,280],[718,560,394,310],[792,624,442,338],[858,666,482,382],[929,711,509,403],[1003,779,565,439],[1091,857,611,461],[1171,911,661,511],[1273,997,715,535],[1367,1059,751,593],[1465,1125,805,625],[1528,1190,868,658],[1628,1264,908,698],[1732,1370,982,742],[1840,1452,1030,790],[1952,1538,1112,842],[2068,1628,1168,898],[2188,1722,1228,958],[2303,1809,1283,983],[2431,1911,1351,1051],[2563,1989,1423,1093],[2699,2099,1499,1139],[2809,2213,1579,1219],[2953,2331,1663,1273]],T=.4,E=100;function D(e){return e?.reason instanceof Error?e.reason:new DOMException(`The operation was aborted`,`AbortError`)}function O(e){if(e?.aborted)throw D(e)}async function k(t,n){if(t)return O(n),new Promise((r,i)=>{let a=new Image,o=setTimeout(()=>{s(),a.src=``,i(Error(`Image load timed out`))},e.requestTimeoutMs),s=()=>{clearTimeout(o),a.onload=null,a.onerror=null,n?.removeEventListener(`abort`,c)},c=()=>{s(),a.src=``,i(D(n))};a.onload=()=>{s(),r(a)},a.onerror=()=>{s(),i(Error(`Image load error`))},n?.addEventListener(`abort`,c,{once:!0}),t.startsWith(`data:`)||(a.crossOrigin=`Anonymous`),a.src=t})}var A=class n{static _resolveOptions(e){let t=Object.assign({},e);Object.keys(n.defaultOptions).forEach(e=>{e in t||Object.defineProperty(t,e,{value:n.defaultOptions[e],enumerable:!0,writable:!0})});let r=t.components&&typeof t.components==`object`?t.components:{};if(t.components=Object.fromEntries(Object.entries(n.defaultComponentOptions).map(([e,t])=>[e,Object.assign({},t,r[e])])),t.dotScale!==null&&t.dotScale!==void 0){if(t.dotScale<=0||t.dotScale>1)throw Error(`dotScale should be in range (0, 1].`);t.components.data.scale=t.dotScale,t.components.timing.scale=t.dotScale,t.components.alignment.scale=t.dotScale}return t}constructor(e,t){O(t);let r=n._resolveOptions(e);this.options=r,this.signal=t,this.canvas=new c(r.size,r.size),this.canvasContext=this.canvas.getContext(`2d`),this.qrCode=new g(-1,this.options.correctLevel),Number.isInteger(this.options.maskPattern)&&(this.qrCode.maskPattern=this.options.maskPattern),Number.isInteger(this.options.version)&&(this.qrCode.typeNumber=this.options.version),this.qrCode.addData(this.options.text),this.qrCode.make(),this._cache=new Map}draw(){return this._draw()}_clear(){this.canvasContext.clearRect(0,0,this.canvas.width,this.canvas.height)}static _getGIFCanvasSize(e,t){let n=Number(e?.lsd?.width)||0,r=Number(e?.lsd?.height)||0;return t.forEach(e=>{let t=e?.dims;t&&(n=Math.max(n,t.left+t.width),r=Math.max(r,t.top+t.height))}),{width:n,height:r}}static _assertGIFBuffer(t){let n=Number(t?.byteLength);if(!Number.isFinite(n)||n<=0)throw Error(`GIF background must contain binary data.`);if(n>e.maxBytes)throw Error(`GIF background encoded size ${n} exceeds the maximum ${e.maxBytes} bytes.`)}static _assertParsedGIF(t){let n=Array.isArray(t?.frames)?t.frames.filter(e=>e?.image?.descriptor):[];if(n.length===0)throw Error(`GIF background must contain at least one frame.`);if(n.length>e.maxFrames)throw Error(`GIF background frame count ${n.length} exceeds the maximum ${e.maxFrames}.`);let r=Number(t?.lsd?.width)||0,i=Number(t?.lsd?.height)||0,a=0;if(n.forEach((e,t)=>{let n=e.image.descriptor;if(![n.left,n.top,n.width,n.height].every(Number.isInteger)||n.left<0||n.top<0||n.width<=0||n.height<=0)throw Error(`GIF background frame ${t} has invalid dimensions.`);r=Math.max(r,n.left+n.width),i=Math.max(i,n.top+n.height),a+=n.width*n.height}),!r||!i)throw Error(`GIF background has invalid dimensions.`);if(r>e.maxDimension||i>e.maxDimension)throw Error(`GIF background dimensions ${r}x${i} exceed the maximum ${e.maxDimension}x${e.maxDimension}.`);if(a>e.maxTotalPixels)throw Error(`GIF background total frame pixels ${a} exceeds the maximum ${e.maxTotalPixels}.`);return{width:r,height:i,totalPixels:a,frameCount:n.length}}static _assertGIFFrames(t,r,i){if(!Array.isArray(r)||r.length===0)throw Error(`GIF background must contain at least one frame.`);if(r.length>e.maxFrames)throw Error(`GIF background frame count ${r.length} exceeds the maximum ${e.maxFrames}.`);let a=i??n._getGIFCanvasSize(t,r),{width:o,height:s}=a;if(!o||!s)throw Error(`GIF background has invalid dimensions.`);if(o>e.maxDimension||s>e.maxDimension)throw Error(`GIF background dimensions ${o}x${s} exceed the maximum ${e.maxDimension}x${e.maxDimension}.`);return r.forEach((e,t)=>{let n=e?.dims;if(!n||n.width<=0||n.height<=0||n.left<0||n.top<0)throw Error(`GIF background frame ${t} has invalid dimensions.`);if(n.left+n.width>o||n.top+n.height>s)throw Error(`GIF background frame ${t} exceeds GIF bounds.`);let r=n.width*n.height*4;if(!e.patch||e.patch.length!==r)throw Error(`GIF background frame ${t} has invalid pixel data.`)}),a}static _decodeGIF(e,t){n._assertGIFBuffer(e);let r=t.parseGIF(e),i=n._assertParsedGIF(r),a=t.decompressFrames(r,!0);return n._assertGIFFrames(r,a,i),{parsedGIF:r,gifFrames:a,gifSize:i}}static _getGIFBackgroundColor(e){let t=Number(e?.lsd?.backgroundColorIndex),n=Number.isInteger(t)?e?.gct?.[t]:void 0,r=n?Array.from(n).slice(0,3):[];return r.length<3||!r.every(Number.isFinite)?null:`rgb(${r[0]},${r[1]},${r[2]})`}static _restoreGIFBackground(e,t,n,r,i,a){e.clearRect(n,r,i,a),t&&(e.fillStyle=t,e.fillRect(n,r,i,a))}static _normalizeGIFFrameDelay(e){let t=Number(e);return!Number.isFinite(t)||t<=0?E:t}static _prepareRoundedCornerClip(e,t,n,r,i,a){e.beginPath(),e.moveTo(t,n),e.arcTo(t+r,n,t+r,n+i,a),e.arcTo(t+r,n+i,t,n+i,a),e.arcTo(t,n+i,t,n,a),e.arcTo(t,n,t+r,n,a),e.closePath()}static _getAverageRGB(e){let t={r:0,g:0,b:0},{width:n,height:r}=e,i=new c(n,r).getContext(`2d`);if(!i)return t;i.drawImage(e,0,0);let a;try{a=i.getImageData(0,0,n,r).data}catch{return t}let o={r:0,g:0,b:0},s=0;for(let e=0;e<a.length;e+=20){let t=a[e],n=a[e+1],r=a[e+2];t>200||n>200||r>200||(s++,o.r+=t,o.g+=n,o.b+=r)}return s===0?t:{r:~~(o.r/s),g:~~(o.g/s),b:~~(o.b/s)}}static _drawDot(e,t,n,r,i=0,a=1){e.fillRect((t+i)*r,(n+i)*r,a*r,a*r)}static _drawAlignProtector(e,t,n,r){e.clearRect((t-2)*r,(n-2)*r,5*r,5*r),e.fillRect((t-2)*r,(n-2)*r,5*r,5*r)}static _drawAlign(e,t,n,r,i=0,a=1,o,s){let c=e.fillStyle;e.fillStyle=o;let l=o=>{o.forEach(([o,s])=>{this._drawDot(e,t+o,n+s,r,i,a)})};l([[-2,-2],[-1,-2],[0,-2],[1,-2],[2,-2],[2,-1],[2,0],[2,1],[2,2],[1,2],[0,2],[-1,2],[-2,2],[-2,1],[-2,0],[-2,-1]]),this._drawDot(e,t,n,r,i,a),s||(e.fillStyle=`rgba(255, 255, 255, 0.6)`,l([[-1,-1],[0,-1],[1,-1],[-1,0],[1,0],[-1,1],[0,1],[1,1]])),e.fillStyle=c}async _draw(){O(this.signal);let e=this.qrCode?.moduleCount,r=this.options.size,i=this.options.margin;(i<0||i*2>=r)&&(i=0);let a=Math.ceil(i),o=r-2*i,s=this.options.whiteMargin,l=this.options.backgroundDimming,u=Math.ceil(o/e),d=u*e,f=d+2*a,p=new c(f,f),m=p.getContext(`2d`);this._clear(),m.save(),m.translate(a,a);let h=new c(f,f),g=h.getContext(`2d`),_=null,v=null,b=[],x;if(this.options.gifBackground){let[e,r]=await Promise.all([t(()=>import(`./gifuct-js-C5F8qDCs.js`),[]),t(()=>import(`./GIFEncoder-BhcmghSn.js`),[])]);x=r.default,O(this.signal);let i=n._decodeGIF(this.options.gifBackground,e);if(_=i.parsedGIF,v=i.gifSize,b=i.gifFrames,O(this.signal),this.options.autoColor){let e=0,t=0,n=0,r=0,i=b[0].colorTable||[];for(let a=0;a<i.length;a++){let o=i[a];o[0]>200||o[1]>200||o[2]>200||o[0]===0&&o[1]===0&&o[2]===0||(r++,e+=o[0],t+=o[1],n+=o[2])}r>0&&(e=~~(e/r),t=~~(t/r),n=~~(n/r),this.options.colorDark=`rgb(${e},${t},${n})`)}}else if(this.options.backgroundImage){let e=await k(this.options.backgroundImage,this.signal);if(this.options.autoColor){let t=n._getAverageRGB(e);this.options.colorDark=`rgb(${t.r},${t.g},${t.b})`}g.drawImage(e,0,0,e.width,e.height,0,0,f,f),g.rect(0,0,f,f),g.fillStyle=l,g.fill()}else g.rect(0,0,f,f),g.fillStyle=this.options.colorLight,g.fill();let S=y.getPatternPosition(this.qrCode.typeNumber),C=this.options.components?.data?.scale||T,w=(1-C)*.5;for(let t=0;t<e;t++)for(let n=0;n<e;n++){let r=this.qrCode.isDark(t,n),i=n<8&&(t<8||t>=e-8)||n>=e-8&&t<8,a=t==6&&n>=8&&n<=e-8||n==6&&t>=8&&t<=e-8,o=i||a;for(let e=1;e<S.length-1;e++)o||=t>=S[e]-2&&t<=S[e]+2&&n>=S[e]-2&&n<=S[e]+2;let s=n*u+(o?0:w*u),c=t*u+(o?0:w*u);if(m.strokeStyle=r?this.options.colorDark:this.options.colorLight,m.lineWidth=.5,m.fillStyle=r?this.options.colorDark:this.options.colorLight,S.length===0)o||m.fillRect(s,c,(o?1:C)*u,(o?1:C)*u);else{let r=n<e-4&&n>=e-4-5&&t<e-4&&t>=e-4-5;!o&&!r&&m.fillRect(s,c,(o?1:C)*u,(o?1:C)*u)}}let E=S[S.length-1];if(m.fillStyle=this.options.colorLight,m.fillRect(0,0,8*u,8*u),m.fillRect(0,(e-8)*u,8*u,8*u),m.fillRect((e-8)*u,0,8*u,8*u),this.options.components?.timing?.protectors&&(m.fillRect(8*u,6*u,(e-8-8)*u,u),m.fillRect(6*u,8*u,u,(e-8-8)*u)),this.options.components?.cornerAlignment?.protectors&&n._drawAlignProtector(m,E,E,u),this.options.components?.alignment?.protectors)for(let e=0;e<S.length;e++)for(let t=0;t<S.length;t++){let r=S[t],i=S[e];r===6&&(i===6||i===E)||i===6&&(r===6||r===E)||r===E&&i===E||n._drawAlignProtector(m,r,i,u)}m.fillStyle=this.options.colorDark,m.fillRect(0,0,7*u,u),m.fillRect((e-7)*u,0,7*u,u),m.fillRect(0,6*u,7*u,u),m.fillRect((e-7)*u,6*u,7*u,u),m.fillRect(0,(e-7)*u,7*u,u),m.fillRect(0,(e-7+6)*u,7*u,u),m.fillRect(0,0,u,7*u),m.fillRect(6*u,0,u,7*u),m.fillRect((e-7)*u,0,u,7*u),m.fillRect((e-7+6)*u,0,u,7*u),m.fillRect(0,(e-7)*u,u,7*u),m.fillRect(6*u,(e-7)*u,u,7*u),m.fillRect(2*u,2*u,3*u,3*u),m.fillRect((e-7+2)*u,2*u,3*u,3*u),m.fillRect(2*u,(e-7+2)*u,3*u,3*u);let D=this.options.components?.timing?.scale||T,A=(1-D)*.5;for(let t=0;t<e-8;t+=2)n._drawDot(m,8+t,6,u,A,D),n._drawDot(m,6,8+t,u,A,D);let M=this.options.components?.cornerAlignment?.scale||T,N=(1-M)*.5;n._drawAlign(m,E,E,u,N,M,this.options.colorDark,this.options.components?.cornerAlignment?.protectors||!1);let P=this.options.components?.alignment?.scale||T,F=(1-P)*.5;for(let e=0;e<S.length;e++)for(let t=0;t<S.length;t++){let r=S[t],i=S[e];r===6&&(i===6||i===E)||i===6&&(r===6||r===E)||r===E&&i===E||n._drawAlign(m,r,i,u,F,P,this.options.colorDark,this.options.components?.alignment?.protectors||!1)}if(s&&(m.fillStyle=this.options.backgroundColor,m.fillRect(-a,-a,f,a),m.fillRect(-a,d,f,a),m.fillRect(d,-a,a,f),m.fillRect(-a,-a,a,f)),this.options.logoImage){let e=await k(this.options.logoImage,this.signal),t=this.options.logoScale,r=this.options.logoMargin,i=this.options.logoCornerRadius;(t<=0||t>=1)&&(t=.2),r<0&&(r=0),i<0&&(i=0);let o=d*t,s=.5*(f-o),c=s;m.restore(),m.fillStyle=this.options.logoBackgroundColor,m.save(),n._prepareRoundedCornerClip(m,s-r,c-r,o+2*r,o+2*r,i+r),m.clip();let l=m.globalCompositeOperation;m.globalCompositeOperation=`destination-out`,m.fill(),m.globalCompositeOperation=l,m.restore(),m.save(),n._prepareRoundedCornerClip(m,s,c,o,o,i),m.clip(),m.drawImage(e,s,c,o,o),m.restore(),m.save(),m.translate(a,a)}if(_){let e=v,t=new x(r,r);t.setRepeat(0);let i=new c(e.width,e.height),a=i.getContext(`2d`),o=new c(f,f),s=o.getContext(`2d`),u=new c(r,r),d=u.getContext(`2d`),m,h,g,y,S,C=n._getGIFBackgroundColor(_);n._restoreGIFBackground(a,C,0,0,i.width,i.height);for(let e of b){O(this.signal),y&&(y.disposalType===2?n._restoreGIFBackground(a,C,y.dims.left,y.dims.top,y.dims.width,y.dims.height):y.disposalType===3&&S&&a.putImageData(S,y.dims.left,y.dims.top),S=void 0),e.disposalType===3&&(S=a.getImageData(e.dims.left,e.dims.top,e.dims.width,e.dims.height));let{width:_,height:v}=e.dims;m&&g&&_===m.width&&v===m.height||(m=new c(_,v),h=m.getContext(`2d`),g=h.createImageData(_,v)),g.data.set(e.patch),h.putImageData(g,0,0),a.drawImage(m.getContext(`2d`).canvas,e.dims.left,e.dims.top),s.clearRect(0,0,f,f),s.drawImage(i,0,0,f,f),s.fillStyle=l,s.fillRect(0,0,f,f),s.drawImage(p.getContext(`2d`).canvas,0,0,f,f),d.clearRect(0,0,r,r),d.drawImage(o,0,0,r,r),t.setDelay(n._normalizeGIFFrameDelay(e.delay)),t.setDispose(e.disposalType??0),t.addFrame(d.getImageData(0,0,u.width,u.height).data),y=e}if(t.finish(),j(this.canvas)){let e=t.stream().toFlattenUint8Array().reduce((e,t)=>e+String.fromCharCode(t),``);return Promise.resolve(`data:image/gif;base64,${globalThis.btoa(e)}`)}return Promise.resolve(t.stream().toFlattenUint8Array())}{g.drawImage(p.getContext(`2d`).canvas,0,0,f,f),m.drawImage(h.getContext(`2d`).canvas,-a,-a,f,f);let e=new c(r,r);e.getContext(`2d`).drawImage(p.getContext(`2d`).canvas,0,0,r,r),this.canvas=e;let t=this.options.gifBackground?`gif`:`png`;return j(this.canvas)?Promise.resolve(this.canvas.toDataURL(t)):Promise.resolve(this.canvas.toBuffer(t))}}_getCachedResult(e,t){if(this._cache.has(e))return this._cache.get(e);let n=t();return this._cache.set(e,n),n}};A.CorrectLevel=n,A.defaultComponentOptions={data:{scale:.4},timing:{scale:.5,protectors:!1},alignment:{scale:.5,protectors:!1},cornerAlignment:{scale:.5,protectors:!0}},A.defaultOptions={text:``,size:400,margin:20,colorDark:`#000000`,colorLight:`rgba(255, 255, 255, 0.6)`,correctLevel:n.M,backgroundImage:void 0,backgroundDimming:`rgba(0,0,0,0)`,logoImage:void 0,logoScale:.2,logoMargin:4,logoCornerRadius:8,whiteMargin:!0,components:A.defaultComponentOptions,autoColor:!0,logoBackgroundColor:`#ffffff`,backgroundColor:`#ffffff`};function j(e){return l(e)}async function M(e,t){return new A(e,t).draw()}export{M as renderQRCodeOnMainThread};