const e=require(`../../types/external-action.types.cjs`),t=require(`../../constants/chains.constants.cjs`),n=require(`../../constants/protocol.constants.cjs`),r=require(`../../types/scheduled-transactions.types.cjs`);require(`../../types/index.cjs`);const i=require(`../../functions/utils/caseInsensitive.utils.cjs`),a=require(`../../functions/utils/addresses.cjs`),o=require(`../../error-handling/error-codes.constants.cjs`),s=require(`../../functions/utils/time.utils.cjs`),c=require(`../utxo/Utxo.cjs`),l=require(`../../functions/utils/tron.utils.cjs`),u=require(`../../functions/utils/token-check.utils.cjs`),d=require(`../../functions/pre-transaction/constructAdminData.cjs`),f=require(`../../functions/pre-transaction/getFeeStructure.cjs`),p=require(`../../functions/pre-transaction/waitForDepositedUtxosInMerkleTree.cjs`),m=require(`../../functions/pre-transaction/buildCommitmentValidationData.cjs`);require(`../../functions/pre-transaction/index.cjs`);const h=require(`../../functions/utils/fees.utils.cjs`),g=require(`../../functions/snarkjs/constructGeneralZkProof.cjs`),_=require(`../../functions/web3/functionCalls/transactCallRelayer.cjs`),v=require(`../../API/deposit-and-withdraw-status-calls.cjs`),y=require(`./hinkalDepositOnChainUtxos.cjs`);require(`../../functions/index.cjs`);var b=async(e,a,u,f,p,y,b,x,S,C,w,T)=>{let E=t.isTronLike(a);if(f.length===0)throw Error(`userDepositedUtxos must not be empty`);let D=u.erc20TokenAddress,[O,k]=await Promise.all([e.getRandomRelay(a),e.getEthereumAddressByChain(a)]);if(!O)throw Error(o.transactionErrorCodes.RELAYER_NOT_AVAILABLE);let A=s.getCurrentTimeInSeconds().toString(),j=e.generateProofRemotely&&!E?5:1,M=[],N=l.addressToHexFormat(k);for(let t=0;t<f.length;t+=j){let r=f.slice(t,t+j),o=await Promise.all(r.map(async({recipientAddress:r,utxo:o},s)=>{let f=t+s,_=l.addressToHexFormat(r),v=new c.Utxo({amount:0n,erc20TokenAddress:D,nullifyingKey:e.userKeys.getShieldedPrivateKey(),timeStamp:A,...o.isNewStyle?{spendingPublicKey:e.userKeys.getSpendingKeyPair().pubSpendingBJJPoint,isNewStyle:!0}:{isNewStyle:!1}}),b=[[i.caseInsensitiveEqual(o.erc20TokenAddress,D)?o:c.Utxo.createFrom(o,{erc20TokenAddress:D}),v]],x=[[v]],S={externalActionId:0n,externalAddress:_,externalActionMetadata:`0x00`},C=d.constructAdminData(w,a,[D],[-o.amount],N),T=await h.calculateModifiedFeeStructure(a,u,p[f],y),[{zkCallData:k,circomData:j,dimData:M},P]=await Promise.all([g.constructZkProof(e.merkleTreeHinkalByChain[a],b,x,e.userKeys,S.externalActionId,S.externalAddress,S.externalActionMetadata,e.generateProofRemotely,T,O??n.zeroAddress,a),m.buildCommitmentValidationData(a,e.userKeys,[D],b)]);return{zkCallData:k,dimData:M,circomData:j,commitmentValidationData:P,adminData:C,recipientAddress:r,tronProofSignature:await l.reorderZkCallData(E,k,M,j)}}));M.push(...o)}await v.safeUpdateDepositAndWithdrawStatus({id:x,chainId:a,hashedEthereumAddress:b,phase:r.DepositAndWithdrawPhase.BEFORE_SCHEDULE_WITHDRAW});let P=await _.transactCallRelayerBatch(a,M,b,S,C,T);return await v.safeUpdateDepositAndWithdrawStatus({id:x,chainId:a,hashedEthereumAddress:b,phase:r.DepositAndWithdrawPhase.AFTER_SCHEDULE_WITHDRAW,scheduleId:P}),P},x=async(t,n,r,i,o,s,c,d,m,h=!0)=>{let g=u.validateAndGetChainId([n]),_=m?a.hashString(m):void 0,v=n.erc20TokenAddress,x=a.hashEthereumAddress(l.addressToHexFormat(await t.getEthereumAddressByChain(g))),S=s??await f.getFeeStructure(g,v,[v],e.ExternalActionId.Transact,[],10n),{userDepositedUtxos:C,statusId:w,depositTxHash:T}=await y.hinkalDepositOnChainUtxos(t,g,n,r,i,S,x,h);return await p.waitForDepositedUtxosInMerkleTree(t,g,C),await b(t,g,n,C,r,S,x,w,o,c,d,_),T};exports.hinkalDepositAndWithdraw=x,exports.hinkalWithdrawBatch=b;