const e=require(`../../types/ethereum-network.types.cjs`),t=require(`../../constants/protocol.constants.cjs`),n=require(`../../functions/utils/caseInsensitive.utils.cjs`),r=require(`../../externalABIs/index.cjs`),i=require(`../../functions/utils/token-check.utils.cjs`),a=require(`../../functions/utils/tx-confirmation.utils.cjs`);var o=(e,t)=>{let n=[],r=0;for(let a=0;a<e.length;a+=1){let o=!1;for(let s=0;s<r;s+=1)if(i.isSameERC20Token(n[s].token,e[a])){n[s].balance+=t[a],o=!0;break}o||(n[r]={token:e[a],balance:t[a]},r+=1)}return n},s=async(r,i,o,s)=>{for(let c=0;c<s.length;c+=1)n.caseInsensitiveEqual(s[c].token.erc20TokenAddress,t.zeroAddress)||await a.waitForEthereumTransactionConfirmation(i,(await r.getContractWithSigner(i,e.ContractType.ERC20Contract,s[c].token.erc20TokenAddress).approve(o,s[c].balance)).hash)},c=async(e,n,i,a)=>{let o=Array.from(a.entries());for(let a=0;a<o.length;a+=1){let[s,c]=o[a],l=await e.contract(r.ERC20ABI,s).allowance(n,i).call({from:n});if(BigInt(String(l))<c){let{transaction:r}=await e.transactionBuilder.triggerSmartContract(s,`approve(address,uint256)`,{feeLimit:t.TRON_DEFAULT_FEE_LIMIT_SUN},[{type:`address`,value:i},{type:`uint256`,value:c.toString()}],n),a=await e.trx.sign(r),o=await e.trx.sendRawTransaction(a);if(!o.result){let e=o.code??`UNKNOWN`,t=o.message?Buffer.from(o.message,`hex`).toString():`no message`;throw Error(`Tron token approval failed: ${e} - ${t}`)}}}},l=e=>{let t=new Set;e.forEach(({extraRandomization:e,stealthAddress:n})=>{let r=`${e.toString()}:${n.toString()}`;if(t.has(r))throw Error(`Duplicate randomization and stealth address pair detected in stealthAddressStructures`);t.add(r)})};exports.aggregateAmountsForApproval=o,exports.approveTokens=s,exports.approveTronTokens=c,exports.assertNoDuplicateStealthAddressStructures=l;