const e=require(`../../types/ethereum-network.types.cjs`),t=require(`../../constants/chains.constants.cjs`),n=require(`../../constants/protocol.constants.cjs`),r=require(`../../functions/utils/caseInsensitive.utils.cjs`),i=require(`../../API/admin-calls.cjs`),a=require(`../../error-handling/logger.cjs`),o=require(`../../functions/utils/tron.utils.cjs`),s=require(`../../functions/utils/token-check.utils.cjs`),c=require(`../../functions/pre-transaction/constructAdminData.cjs`),l=require(`../../functions/utils/getRecipientInfoFromUserKeys.cjs`),u=require(`../../functions/utils/getContractAddress.cjs`),d=require(`../../functions/utils/tronSimulation.utils.cjs`),f=require(`../../functions/web3/functionCalls/constructBatchCall.cjs`),p=require(`../../functions/web3/functionCalls/waitForErc20Approvals.cjs`),m=require(`./hinkalProoflessDeposit.utils.cjs`),h=require(`./hinkalProoflessDepositViaWrapper.cjs`);var g=async(e,r,i,s,c,l=!1)=>{let u=e.getTronWeb(),{hinkalAddress:f}=t.networkRegistry[r].contractData,h=o.evmHexToTronBase58Address(f),g=await e.getEthereumAddressByChain(r),_=i.map(e=>e.erc20TokenAddress),v=i.map(()=>0n),{totalTrxValue:y,tokensNeedingApproval:b}=o.splitTronTokensByNative(i,s),x=c.map(e=>({extraRandomization:e.extraRandomization.toString(),stealthAddress:e.stealthAddress.toString(),H0:e.H0.toString(),H1:e.H1.toString()}));if(y>0n){let{hinkalABI:e}=t.networkRegistry[r].contractData;if(!e)throw Error(`Hinkal ABI not found`);await o.assertTronSufficientBalance(u,r,h,g,o.createTronCallData(e,`prooflessDeposit`,[_,s.map(String),v.map(String),c.map(e=>[e.extraRandomization.toString(),e.stealthAddress.toString(),e.H0.toString(),e.H1.toString()])]),y)}let{transaction:S}=await u.transactionBuilder.triggerSmartContract(h,`prooflessDeposit(address[],uint256[],uint256[],(uint256,uint256,uint256,uint256)[])`,{feeLimit:n.TRON_DEFAULT_FEE_LIMIT_SUN,callValue:y>0n?Number(y):0},[{type:`address[]`,value:_},{type:`uint256[]`,value:s.map(String)},{type:`uint256[]`,value:v.map(String)},{type:`tuple(uint256,uint256,uint256,uint256)[]`,value:x.map(e=>[e.extraRandomization,e.stealthAddress,e.H0,e.H1])}],g);if(l)return S;await m.approveTronTokens(u,g,h,b),await p.waitForTronErc20Approvals(u,g,h,Array.from(b.entries()).map(([e,t])=>({tokenAddress:e,requiredAmount:t}))),await d.simulateTronTransaction(r,h,`prooflessDeposit`,t.networkRegistry[r].contractData.hinkalABI,[_,s.map(String),v.map(String),c.map(e=>[e.extraRandomization.toString(),e.stealthAddress.toString(),e.H0.toString(),e.H1.toString()])],g,y>0n?y:void 0);let C=await u.trx.sign(S),w=await u.trx.sendRawTransaction(C);if(!w.result)throw a.Logger.error(`Tron prooflessDeposit broadcast failed`,w),Error(`Tron prooflessDeposit broadcast failed`);let T=typeof w==`string`?w:w?.txid??w?.transaction?.txID;if(!T)throw Error(`Tron prooflessDeposit failed (missing txid)`);return T},_=async(a,o,d,h,_,v=!1)=>{let y=s.validateAndGetChainId(o),b=t.isTronLike(y),x=await a.getEthereumAddressByChain(y),S=h??o.map(()=>l.getStealthAddressStructureFromUserKeys(a.userKeys));if(S.length!==o.length)throw Error(`Stealth address structures length must be equal to erc20 tokens length`);m.assertNoDuplicateStealthAddressStructures(S);let C;if(b){let e=await g(a,y,o,d,S,v);if(v)return e;C=e}else{let t=a.getContractWithSigner(y,e.ContractType.HinkalContract),i=[o.map(({erc20TokenAddress:e})=>e),d,o.map(()=>0n),S];if(v)return t.prooflessDeposit.populateTransaction(...i);let s=a.getProviderAdapter(y),c=m.aggregateAmountsForApproval(o,d),l=o.reduce((e,t,i)=>r.caseInsensitiveEqual(t.erc20TokenAddress,n.zeroAddress)?e+d[i]:e,0n),h=c.filter(({token:e,balance:t})=>!r.caseInsensitiveEqual(e.erc20TokenAddress,n.zeroAddress)&&t>0n),g;if(h.length>0&&await s.supportsBatchCall?.(y)&&s.sendBatchCallsTransaction){let e=f.buildApproveAndProoflessDepositCalls(h.map(({token:e})=>e.erc20TokenAddress),h.map(({balance:e})=>e),u.getContractAddress(t),t,...i,l);g=await s.sendBatchCallsTransaction(y,e)}else{await m.approveTokens(a,y,u.getContractAddress(t),c);let e=await a.getEthereumAddressByChain(y),n=c.map(({token:e,balance:t})=>({tokenAddress:e.erc20TokenAddress,requiredAmount:t}));await p.waitForErc20Approvals(a,y,e,u.getContractAddress(t),n),g=await t.prooflessDeposit(...i,{value:l>0n?l:void 0})}C=g.hash}return i.emitTxPublicData(c.constructAdminData(_,y,o.map(({erc20TokenAddress:e})=>e),d,x)),C},v=async(e,n,r,i,a,o,s)=>{let c=i.map(()=>r),l=r.erc20TokenAddress;return t.isTronLike(n)?h.hinkalTronProoflessDepositViaWrapper(e,n,l,o,c,i,a,s):t.networkRegistry[n]?.contractData?.hinkalWrapperAddress?h.hinkalEvmProoflessDepositViaWrapper(e,n,l,o,c,i,a,s):await _(e,c,i,a,s,!0)};exports.hinkalProoflessDeposit=_,exports.hinkalProoflessDepositWithPublicFee=v;