import loader from"@assemblyscript/loader";import{FileLoader}from"three";const StaticCode=[0,97,115,109,1,0,0,0,1,8,1,96,3,127,127,127,1,127,3,2,1,0,4,5,1,112,1,1,1,5,3,1,0,0,6,20,3,127,0,65,8,11,127,1,65,136,128,2,11,127,0,65,136,128,2,11,7,20,2,7,100,101,99,114,121,112,116,0,0,6,109,101,109,111,114,121,2,0,9,6,1,0,65,1,11,0,10,21,1,19,0,32,0,32,1,115,32,2,65,255,1,113,115,65,255,1,113,15,11];export default class Decrypt{constructor(a){this.buffer=void 0,this.date=void 0,this.loading=!1,this.queue=[],this.config=void 0,this.config=a}get enable(){return this.config.enable}get decryptBuffer(){const a=new Date().getDate();return this.date===a?this.buffer:void 0}bufferToString(a){const b=new Uint8Array(a),c=new TextDecoder("utf-8");return c.decode(b)}stringToArrayBuffer(a){const b=new TextEncoder;return b.encode(a)}loadDecryptKey(a){if(this.decryptBuffer)return void a();if(this.queue.push(a),this.loading)return;this.loading=!0;const b=a=>{this.queue.forEach(b=>b(a)),this.queue=[],this.loading=!1};new FileLoader().load(this.config.url.keyPath,a=>{const c=JSON.parse(a.replace(/\n/g,"<n>").replace(/\r/g,"<r>"));if(200==c.code)this.buffer=this.stringToArrayBuffer(c.data.replace(/<n>/g,"\n").replace(/<r>/g,"\r")),this.date=new Date().getDate();else if(400==c.code)window.location.href=this.config.url.licensePage;else throw Error(a);b()},void 0,a=>{b(a)})}parser(a,b=512){return this.config.enable?new Promise((c,d)=>{this.loadDecryptKey(e=>{if(e)return void d(e);const f=Uint8Array.from(StaticCode);loader.instantiate(f.buffer,{imports:{},env:{abort:()=>{}}}).then(({exports:d})=>{const{decrypt:e}=d,f=Math.min(a.length,b),g=this.decryptBuffer,h=g.length;for(let b=0;b<f;)a[b]=e(a[b],g[b%h],b),b++;c(a.buffer)}).catch(d)})}):Promise.resolve(a.buffer)}}