const e=require(`./workerProxy-BNBqYien.js`),t=require(`./logError-fCJ4HXQz.js`);let n=require(`worker_threads`),r=require(`buffer`);var i=e.C(e.v());Object.values({preHookContract:`0x0000000000000000000000000000000000000000`,hookContract:`0x0000000000000000000000000000000000000000`,preHookMetadata:`0x00`,postHookMetadata:`0x00`});var a=(e,t)=>{let n=[];for(let r=0;r<e.length;r+=1){n.push([]);for(let i=0;i<e[r].length;i+=1)n[r][i]=t.getSiblingHashesForVerification(BigInt(e[r][i].getCommitment())).map(e=>e.toString())}let r=[];for(let n=0;n<e.length;n+=1){r.push([]);for(let i=0;i<e[n].length;i+=1)r[n][i]=t.getSiblingSides(BigInt(e[n][i].getCommitment())).map(e=>e.toString())}return{inCommitmentSiblings:n,inCommitmentSiblingSides:r}},o=function(e){return e[e.CALC_COMMITMENTS_SIBLING_AND_SIDES=0]=`CALC_COMMITMENTS_SIBLING_AND_SIDES`,e[e.BUILD_IN_NULLIFIERS=1]=`BUILD_IN_NULLIFIERS`,e}({}),s=class extends Error{constructor(){super(`Merkle tree is incomplete`),this.name=`MerkleTreeIncompleteError`}},c=class e{tree;count;index;reverseTree;static createWithData(t,n,r,i,a,o,s){return new e(a,o,s,t,n,r,i)}static create(n,r,i=t.u){return new e(n,i,r)}createReverseTree(){let e=new Map;for(let t=this.getStartIndex();t<this.getStartIndex()+this.count;t+=1n){let n=this.tree.get(t);n!==void 0&&e.set(n,t)}return e}constructor(e,t,n,r,i,a,o){this.hashFunction=e,this.levels=t,this.defaultNodeValue=n,this.tree=r??new Map,this.count=o??0n,this.index=a??2n**(t-1n),i?this.reverseTree=i:this.reverseTree=this.createReverseTree()}getStartIndex(){return 2n**(this.levels-1n)}logarithm2(e){let t=0n;for(;2n**t<e;)t+=1n;return t}bigIntMax(e,t){return e>t?e:t}forceInsert(e,t){if(t<this.getStartIndex())throw RangeError();this.tree.set(t,e),this.reverseTree.set(e,t);let n=this.index-this.getStartIndex(),r=this.logarithm2(n),i=t;for(let e=1n;e<=r;e+=1n){i/=2n;let e=this.hashFunction(this.tree.get(i*2n)||this.defaultNodeValue,this.tree.get(i*2n+1n)||this.defaultNodeValue);this.tree.set(i,e)}}insert(e,n){let r=!1;this.tree.has(n)&&(r=!0,t.t(`Editing commitment index: ${n} `)),r||(this.count+=1n),this.index=this.bigIntMax(this.index,n+1n),this.forceInsert(e,n)}remove(e){this.forceInsert(this.defaultNodeValue,e)}completenessCheck(){if(this.count!==this.index-this.getStartIndex())throw new s}getRootHash(){this.completenessCheck();for(let e=1n;e<2n**this.levels;e*=2n)if(this.tree.get(e))return this.tree.get(e);return this.defaultNodeValue}getMerkleData(){return this.completenessCheck(),new Map(this.tree)}getValue(e){return this.tree.get(e)}getSiblingIndex(e){return e===1n?1n:e%2n==1n?e-1n:e+1n}getSiblingHashesForVerification(e){this.completenessCheck();let t=this.reverseTree.get(e);if(t===void 0)return Array(25).fill(0n);let n=[];for(;t!==0n;)n.push(this.tree.get(this.getSiblingIndex(t))||this.defaultNodeValue),t/=2n;return n.slice(0,25)}getSiblingSides(e){this.completenessCheck();let t=this.reverseTree.get(e);if(t===void 0)return Array(25).fill(0n);let n=[];for(;t!==0n;){let e=t%2n==0n?0n:1n;n.push(e),t/=2n}return n.slice(0,25)}toJSON(){let{tree:e,reverseTree:t,count:n,index:r}=this;return{tree:Object.fromEntries(e),reverseTree:Object.fromEntries(t),count:n.toString(),index:r.toString()}}toJSONLite(){let{tree:e,count:t,index:n}=this;return{tree:Object.fromEntries(e),count:t.toString(),index:n.toString()}}clone(){return e.createWithData(new Map(this.tree),new Map(this.reverseTree),this.index,this.count,this.hashFunction,this.levels,this.defaultNodeValue)}getIndex(){return this.index}},l=e=>new Map(Object.entries(e).map(([e,t])=>[BigInt(e),BigInt(t)])),u=e=>{let n=l(e.tree),r=e.reverseTree?l(e.reverseTree):void 0;return c.createWithData(n,r,BigInt(e.index),BigInt(e.count),t.p,t.u,0n)},d=new e.t,f=e=>{let{inputUtxosSerialized:n,merkleTreeSerialized:r}=e.data,i=u(r),{inCommitmentSiblings:o,inCommitmentSiblingSides:s}=a(n.map(e=>e.map(e=>new t.o(e))),i);d.postMessageToMainThread({inCommitmentSiblings:o,inCommitmentSiblingSides:s})},p=e=>{let{inputUtxosSerialized:n}=e.data,r=n.map(e=>e.map(e=>new t.o(e))).map(e=>e.map(e=>e.amount===0n?`0`:e.getNullifier()));d.postMessageToMainThread(r)},m=async e=>{try{await t.f(!0);let{type:n}=e.payload;switch(n){case o.CALC_COMMITMENTS_SIBLING_AND_SIDES:f(e.payload);return;case o.BUILD_IN_NULLIFIERS:p(e.payload);return;default:throw Error(`Unknown worker message type ${n}`)}}catch(e){console.error(`zk proof worker message failed`,e),d.postErrorToMainThread(e)}};globalThis.process=i.default,globalThis.Buffer=r.Buffer,n.parentPort.on(`message`,async e=>{await m(e)});