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`);require(`../../types/index.cjs`);const i=require(`../../error-handling/error-codes.constants.cjs`),a=require(`../../functions/utils/time.utils.cjs`),o=require(`../../functions/utils/tron.utils.cjs`),s=require(`../../functions/utils/token-check.utils.cjs`),c=require(`../../functions/web3/events/getShieldedBalance.cjs`),l=require(`../../functions/pre-transaction/outputUtxoProcessing.cjs`),u=require(`../../functions/utils/fees.utils.cjs`),d=require(`../../functions/pre-transaction/getVolatileTransferAmount.cjs`),f=require(`../../functions/pre-transaction/constructAdminData.cjs`),p=require(`../../functions/pre-transaction/getFeeStructure.cjs`),m=require(`../../functions/pre-transaction/getKycAndSignatureData.cjs`),h=require(`../../functions/pre-transaction/buildCommitmentValidationData.cjs`);require(`../../functions/pre-transaction/index.cjs`);const g=require(`../../functions/snarkjs/constructGeneralZkProof.cjs`),_=require(`../../functions/web3/functionCalls/transactCallDirect.cjs`),v=require(`../../functions/web3/functionCalls/transactCallRelayer.cjs`),y=require(`../../functions/pre-transaction/merge-with-fee-structure.cjs`);var b=async(e,t,n,r)=>{let i=[...await c.addPaddingToUtxos(e,t,n,r)],o=[],s=a.getCurrentTimeInSeconds().toString();for(let t=0;t<n.length;t+=1){let{outputUtxos:n}=l.outputUtxoProcessing(e.userKeys,i[t],r[t],s);o.push(n)}return{inputUtxosArray:i,outputUtxosArray:o}},x=async(a,c,l,x,S,C,w,T)=>{let E=s.validateAndGetChainId(c),D=e.isTronLike(E),O=o.addressToHexFormat(await a.getEthereumAddressByChain(E)),k=c.map(e=>e.erc20TokenAddress),A=await d.modifyVolatileTokenAmountChanges(E,c,l),j;if(!S){let e=w??await p.getFeeStructure(E,C,k,r.ExternalActionId.Transact),t=u.calculateTotalFee(-A[0],e);j={feeToken:e.feeToken,flatFee:t,variableRate:0n},y.mergeWithFeeStructure(E,k,A,j)}let M=c[0];if(!M)throw Error(`Deposit Action: No Token Found`);let N={externalActionId:0n,externalAddress:o.addressToHexFormat(x),externalActionMetadata:`0x00`},[P,{patchAccessTokenMerkleTree:F,signatureData:I},{inputUtxosArray:L,outputUtxosArray:R}]=await Promise.all([a.getRandomRelay(E,!0),m.getKycAndSignatureData(a,E,k,A,O),b(a,E,k,A)]);if(!S&&!P)throw Error(i.transactionErrorCodes.RELAYER_NOT_AVAILABLE);let z=`swapperM${A.length.toString()}x${L[0].length}x1`,[{zkCallData:B,circomData:V,dimData:H},U]=await Promise.all([g.constructZkProof(a.merkleTreeHinkalByChain[E],a.merkleTreeAccessTokenByChain[E],L,R,a.userKeys,z,N.externalActionId,N.externalAddress,N.externalActionMetadata,a.generateProofRemotely,S?t.zeroAddress:P,E,void 0,void 0,void 0,F,void 0,j,a.getContractWithFetcher(E,n.ContractType.HinkalHelperContract),I,S?O:void 0),h.buildCommitmentValidationData(E,a.userKeys,k,L)]),W=await o.reorderZkCallData(D,B,H,V);if(S){if(!M)throw Error(`Withdraw Action: No Token Found`);return await _.transactCallDirect(a,E,A[0],M,B,V,H)}return await v.transactCallRelayer(E,B,H,V,U,void 0,void 0,f.constructAdminData(T,E,k,A,O),W)};exports.hinkalWithdraw=x;