const e=require(`../../../constants/chains.constants.cjs`),t=require(`../../../constants/protocol.constants.cjs`),n=require(`../../utils/caseInsensitive.utils.cjs`),r=require(`../../../externalABIs/index.cjs`),i=require(`../../../error-handling/error-codes.constants.cjs`),a=require(`../../../error-handling/logger.cjs`),o=require(`../../utils/tron.utils.cjs`),s=require(`../../utils/tronSimulation.utils.cjs`),c=require(`./waitForErc20Approvals.cjs`);var l=async(l,u,d,f,p,m,h,g,_,v=!0)=>{let y=Array.isArray(d)?d:[d],b=Array.isArray(f)?f:[f];if(y.length!==b.length)throw Error(`token and amount length mismatch`);let x=l.getTronWeb(),S=await l.getEthereumAddressByChain(u),{hinkalAddress:C,hinkalABI:w}=e.networkRegistry[u].contractData,T=x.contract(w,o.evmHexToTronBase58Address(C)),E=g??T,D=_??T;if(!E.address)throw Error(`contractForApproval address not set`);if(!D.address)throw Error(`contractForTransaction address not set`);let O=b.findIndex(({erc20TokenAddress:e})=>n.caseInsensitiveEqual(e,t.zeroAddress)),k=O===-1?0n:y[O],A=k>0n?k:0n;if(b.some(({erc20TokenAddress:e},r)=>!n.caseInsensitiveEqual(e,t.zeroAddress)&&y[r]>0n)){let e=E.address;if(!e)throw Error(`token approval spender not set`);let i=[];for(let a=0;a<b.length;a+=1){let s=b[a],c=y[a];if(c>0n&&!n.caseInsensitiveEqual(s.erc20TokenAddress,t.zeroAddress)){let n=o.evmHexToTronBase58Address(s.erc20TokenAddress),a=await x.contract(r.ERC20ABI,n).allowance(S,e).call({from:S});if(BigInt(String(a))<c){let{transaction:r}=await x.transactionBuilder.triggerSmartContract(n,`approve(address,uint256)`,{feeLimit:t.TRON_DEFAULT_FEE_LIMIT_SUN},[{type:`address`,value:e},{type:`uint256`,value:c.toString()}],S),a=await x.trx.sign(r),o=await x.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}`)}i.push({tokenAddress:n,requiredAmount:c})}}}i.length>0&&await c.waitForTronErc20Approvals(x,S,e,i)}let{a:j,b:M,c:N}=o.parseZkCalldata(p),P=await o.reorderZkCallData(!0,p,h,m,!1);if(!P)throw Error(`Tron proof signature not found`);let F=[P.v,P.r,P.s],I=[h.tokenNumber,h.nullifierAmount,h.outputAmount],L=o.circomDataToArray(m);if(v){await s.simulateTronTransaction(u,D.address,`transact`,D.abi,[F,j,M,N,I,L],S,A);let e=BigInt(await x.trx.getBalance(S)),n=BigInt(t.TRON_DEFAULT_FEE_LIMIT_SUN);try{let e=o.createTronCallData(D.abi,`transact`,[F,j,M,N,I,L]);n=await o.estimateTronFeeSunWithPadding(x,u,D.address,S,e,A)}catch(e){a.Logger.error(`Error estimating Tron transact fee`,e)}if(e<A+n)throw Error(i.insufficientResourcesErrorCodes.INSUFFICIENT_TRON_BALANCE_FOR_FEE)}let R=await D.transact(F,j,M,N,I,L).send({feeLimit:t.TRON_DEFAULT_FEE_LIMIT_SUN,callValue:Number(A)}),z=typeof R==`string`?R:R?.txid??R?.transaction?.txID??R?.transaction?.txid;if(!z)throw Error(`Tron transact failed (missing txid)`);return z},u=async(e,r,i,a,s,c,l,u,d)=>{let f=e.getTronWeb(),p=await e.getEthereumAddressByChain(r),m=n.caseInsensitiveEqual(a.erc20TokenAddress,t.zeroAddress),{a:h,b:g,c:_}=o.parseZkCalldata(s),v=await o.reorderZkCallData(!0,s,l,c,!1);if(!v)throw Error(`Tron proof signature not found`);let y=o.createTronCallData(d,`transact`,[[v.v,v.r,v.s],h,g,_,[l.tokenNumber,l.nullifierAmount,l.outputAmount],o.circomDataToArray(c)]),{transaction:b}=await f.transactionBuilder.triggerSmartContract(o.evmHexToTronBase58Address(u),``,{feeLimit:t.TRON_DEFAULT_FEE_LIMIT_SUN,callValue:m?Number(i):0,input:y},[],p);return b};exports.buildTronUnsignedTransactTransaction=u,exports.transactCallDirectTron=l;