const e=require(`../../constants/vite.constants.cjs`),t=require(`../utils/amounts.utils.cjs`),n=require(`../../crypto/poseidon.cjs`),r=require(`../../webworker/worker.registry.cjs`),i=require(`../../webworker/performTaskWithWorker.cjs`),a=require(`../../webworker/zkProofWorker/zkProofWorker.types.cjs`),o=require(`../../data-structures/merkle-tree/MerkleTree.cjs`),s=require(`../../data-structures/merkle-tree/getPatchedAccessTokenMerkleTree.cjs`);require(`../../data-structures/merkle-tree/index.cjs`);const c=require(`./fetchMerkleTreeSiblings.cjs`),l=require(`./fetchOnChainRootHashes.cjs`);var u=async(e,t)=>{let n=e.getRootHash();if(n===void 0)throw Error(`Root hash not available from patched merkle tree`);let o=await i.performTaskWithWorker({type:r.WorkerVariant.ZKProof,payload:{type:a.ZKProofWorkerActionType.CALC_ACCESS_TOKEN_SIBLING_AND_SIDES,data:{accessKey:t,merkleTreeAccessTokenSerialized:e.toJSON()}}});return{accessTokenSiblings:o.accessTokenSiblings,accessTokenSiblingSides:o.accessTokenSiblingSides,rootHashAccessToken:n}},d=async(t,n,o,c,l,d)=>{let f=o.getAccessKey(),p=s.getPatchedAccessTokenMerkleTree(n,f,l,d),m=t.getRootHash();if(m===void 0)throw Error(`Root hash not available from hinkal merkle tree`);let h,g;return e.isNode?(h=await u(p,f),g=await i.performTaskWithWorker({type:r.WorkerVariant.ZKProof,payload:{type:a.ZKProofWorkerActionType.CALC_COMMITMENTS_SIBLING_AND_SIDES,data:{inputUtxosSerialized:c,merkleTreeSerialized:t.toJSON()}}})):[h,g]=await Promise.all([u(p,f),i.performTaskWithWorker({type:r.WorkerVariant.ZKProof,payload:{type:a.ZKProofWorkerActionType.CALC_COMMITMENTS_SIBLING_AND_SIDES,data:{inputUtxosSerialized:c,merkleTreeSerialized:t.toJSON()}}})]),{inCommitmentSiblings:g.inCommitmentSiblings,inCommitmentSiblingSides:g.inCommitmentSiblingSides,accessTokenSiblings:h.accessTokenSiblings,accessTokenSiblingSides:h.accessTokenSiblingSides,rootHashHinkal:m,rootHashAccessToken:h.rootHashAccessToken}},f=async(e,r,i,a,l)=>{let d=r.getAccessKey();if(!l||a){let r=s.getPatchedAccessTokenMerkleTree(o.MerkleTree.create(n.poseidonFunction,0n),d,a,l),[f,p]=await Promise.all([u(r,d),c.fetchMerkleTreeSiblings(e,i,d)]);return{inCommitmentSiblings:p.inCommitmentSiblings,inCommitmentSiblingSides:p.inCommitmentSiblingSides,accessTokenSiblings:f.accessTokenSiblings,accessTokenSiblingSides:f.accessTokenSiblingSides,rootHashHinkal:t.toBigInt(p.rootHashHinkal),rootHashAccessToken:f.rootHashAccessToken}}let f=await c.fetchMerkleTreeSiblings(e,i,d);return{inCommitmentSiblings:f.inCommitmentSiblings,inCommitmentSiblingSides:f.inCommitmentSiblingSides,accessTokenSiblings:f.accessTokenSiblings,accessTokenSiblingSides:f.accessTokenSiblingSides,rootHashHinkal:t.toBigInt(f.rootHashHinkal),rootHashAccessToken:t.toBigInt(f.rootHashAccessToken)}},p=async(e,t,n)=>{if(!t||!n)return!1;try{let r=t.getRootHash(),i=n.getRootHash();if(r===void 0||i===void 0)return!1;let{hinkalRootHash:a,accessTokenRootHash:o}=await l.fetchOnChainRootHashes(e);return r===a&&i===o}catch{return!1}},m=async(e,t,n,r,i,a,o)=>await p(e,a,o)?d(a,o,t,n,r,i):f(e,t,n,r,i),h=async(t,n,o,s,c,l,u)=>{if(e.isNode){let{inCommitmentSiblings:e,inCommitmentSiblingSides:d,accessTokenSiblings:f,accessTokenSiblingSides:p,rootHashHinkal:h,rootHashAccessToken:g}=await m(t,s,c,l,u,n,o);return{inCommitmentSiblings:e,inCommitmentSiblingSides:d,accessTokenSiblings:f,accessTokenSiblingSides:p,rootHashHinkal:h,rootHashAccessToken:g,inNullifiers:await i.performTaskWithWorker({type:r.WorkerVariant.ZKProof,payload:{type:a.ZKProofWorkerActionType.BUILD_IN_NULLIFIERS,data:{inputUtxosSerialized:c}}})}}else{let[{inCommitmentSiblings:e,inCommitmentSiblingSides:d,accessTokenSiblings:f,accessTokenSiblingSides:p,rootHashHinkal:h,rootHashAccessToken:g},_]=await Promise.all([m(t,s,c,l,u,n,o),i.performTaskWithWorker({type:r.WorkerVariant.ZKProof,payload:{type:a.ZKProofWorkerActionType.BUILD_IN_NULLIFIERS,data:{inputUtxosSerialized:c}}})]);return{inCommitmentSiblings:e,inCommitmentSiblingSides:d,accessTokenSiblings:f,accessTokenSiblingSides:p,rootHashHinkal:h,rootHashAccessToken:g,inNullifiers:_}}};exports.getDataFromWorkers=h;