const e=require(`../../constants/chains.constants.cjs`),t=require(`../../constants/protocol.constants.cjs`),n=require(`../../types/hinkal.types.cjs`),r=require(`../../functions/utils/addresses.cjs`),i=require(`../../error-handling/error-codes.constants.cjs`),a=require(`../../API/admin-calls.cjs`),o=require(`../../functions/utils/time.utils.cjs`),s=require(`../utxo/Utxo.cjs`),c=require(`../../functions/utils/tron.utils.cjs`),l=require(`../../functions/utils/token-check.utils.cjs`),u=require(`../../functions/web3/events/getShieldedBalance.cjs`),d=require(`../../functions/pre-transaction/outputUtxoProcessing.cjs`),f=require(`../../functions/pre-transaction/constructAdminData.cjs`),p=require(`../../functions/utils/tronSimulation.utils.cjs`),m=require(`../../functions/snarkjs/constructGeneralZkProof.cjs`),h=require(`../../functions/web3/functionCalls/waitForErc20Approvals.cjs`),g=require(`../../functions/web3/functionCalls/transactCallDirect.cjs`);var _=async(e,t)=>({externalActionData:{externalActionId:0n,externalAddress:c.addressToHexFormat(await e.getEthereumAddressByChain(t)),externalActionMetadata:`0x00`},contractTransaction:void 0,contractToApprove:void 0}),v=async(e,t,n,r)=>{let i=[...await u.addPaddingToUtxos(e,t,n,r)],a=[],s=o.getCurrentTimeInSeconds().toString();for(let t=0;t<n.length;t+=1){let{outputUtxos:n}=d.outputUtxoProcessing(e.userKeys,i[t],r[t],s);a.push(n)}return{inputUtxosArray:i,outputUtxosArray:a}},y=async(n,r,i,a,o,s,l,u=!0,d=!1)=>{let f=n.getTronWeb(),{hinkalAddress:m,hinkalABI:g}=e.networkRegistry[r].contractData;if(!g)throw Error(`Hinkal ABI not found`);let _=c.evmHexToTronBase58Address(m),v=await n.getEthereumAddressByChain(r),{totalTrxValue:y,tokensNeedingApproval:b}=c.splitTronTokensByNative(i,a),{a:x,b:S,c:C}=c.parseZkCalldata(o),w=await c.reorderZkCallData(!0,o,l,s,!1);if(!w)throw Error(`Tron proof signature not found`);let T=[w.v,w.r,w.s],E=[l.tokenNumber,l.nullifierAmount,l.outputAmount],D=c.circomDataToArray(s),O=y>0n?Number(y):0;if(d){let e=c.createTronCallData(g,`transact`,[T,x,S,C,E,D]),{transaction:n}=await f.transactionBuilder.triggerSmartContract(_,``,{feeLimit:t.TRON_DEFAULT_FEE_LIMIT_SUN,callValue:O,input:e},[],v);return n}await c.approveTronTokens(f,v,_,b),b.size>0&&await h.waitForTronErc20Approvals(f,v,_,Array.from(b.entries()).map(([e,t])=>({tokenAddress:e,requiredAmount:t}))),u&&(await p.simulateTronTransaction(r,_,`transact`,g,[T,x,S,C,E,D],v,y>0n?y:void 0),await c.assertTronSufficientBalance(f,r,_,v,c.createTronCallData(g,`transact`,[T,x,S,C,E,D]),y));let k=await f.contract(g,_).transact(T,x,S,C,E,D).send({feeLimit:t.TRON_DEFAULT_FEE_LIMIT_SUN,callValue:O}),A=typeof k==`string`?k:k?.txid??k?.transaction?.txID;if(!A)throw Error(`Tron deposit failed (missing txid)`);return A},b=async(r,i,a,o=!0,s=!1)=>{let c=l.validateAndGetChainId(i),u=e.isTronLike(c),d=i.map(e=>e.erc20TokenAddress),[{externalActionData:f,contractTransaction:p,contractToApprove:h},{inputUtxosArray:b,outputUtxosArray:x}]=await Promise.all([_(r,c),v(r,c,d,a)]),{zkCallData:S,circomData:C,dimData:w}=await m.constructZkProof(r.merkleTreeHinkalByChain[c],b,x,r.userKeys,f.externalActionId,f.externalAddress,f.externalActionMetadata,r.generateProofRemotely,n.zeroFeeStructure,t.zeroAddress,c);return u?await y(r,c,i,a,S,C,w,o,s):await g.transactCallDirect(r,c,a,i,S,C,w,h,p,o,s)},x=async(o,c,u,d,p=!0,h=!1,v)=>{let b=l.validateAndGetChainId(c),x=e.isTronLike(b),S=c.map(e=>e.erc20TokenAddress),[C,w,T,,E]=d.split(`,`);if(!r.isValidPrivateAddress(d))throw Error(i.transactionErrorCodes.RECIPIENT_FORMAT_INCORRECT);let D=S.map(e=>[new s.Utxo({amount:0n,erc20TokenAddress:e,nullifyingKey:o.userKeys.getShieldedPrivateKey(),spendingPublicKey:o.userKeys.getSpendingKeyPair().pubSpendingBJJPoint,isNewStyle:!0}),new s.Utxo({amount:0n,erc20TokenAddress:e,nullifyingKey:o.userKeys.getShieldedPrivateKey(),spendingPublicKey:o.userKeys.getSpendingKeyPair().pubSpendingBJJPoint,isNewStyle:!0})]),O=S.map((e,t)=>[new s.Utxo({amount:u[t],erc20TokenAddress:e,H0:[BigInt(w),BigInt(T)],stealthAddress:C,encryptionKey:E,isNewStyle:!0})]),{externalActionData:k,contractTransaction:A,contractToApprove:j}=await _(o,b),{zkCallData:M,circomData:N,dimData:P}=await m.constructZkProof(o.merkleTreeHinkalByChain[b],D,O,o.userKeys,k.externalActionId,k.externalAddress,k.externalActionMetadata,o.generateProofRemotely,n.zeroFeeStructure,t.zeroAddress,b);if(x)return await y(o,b,c,u,M,N,P,p,h);let F=await g.transactCallDirect(o,b,u,c,M,N,P,j,A,p,h);return a.emitTxPublicData(f.constructAdminData(v,b,S,u,await o.getEthereumAddress())),F};exports.hinkalDeposit=b,exports.hinkalDepositForOther=x;