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