const e=require(`../../types/external-action.types.cjs`),t=require(`../../constants/chains.constants.cjs`),n=require(`../../constants/protocol.constants.cjs`),r=require(`../../types/hinkal.types.cjs`),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/pre-transaction/getVolatileTransferAmount.cjs`),d=require(`../../functions/pre-transaction/constructAdminData.cjs`),f=require(`../../functions/pre-transaction/getFeeStructure.cjs`),p=require(`../../functions/pre-transaction/buildCommitmentValidationData.cjs`);require(`../../functions/pre-transaction/index.cjs`);const m=require(`../../functions/utils/fees.utils.cjs`),h=require(`../../functions/snarkjs/constructGeneralZkProof.cjs`),g=require(`../../functions/web3/functionCalls/transactCallDirect.cjs`),_=require(`../../functions/web3/functionCalls/transactCallRelayer.cjs`),v=require(`../../functions/web3/functionCalls/transactCallDirectTron.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=t.isTronLike(E),O=o.addressToHexFormat(await a.getEthereumAddressByChain(E)),k=c.map(e=>e.erc20TokenAddress),A=await u.modifyVolatileTokenAmountChanges(E,c,l),j=c[0];if(!j)throw Error(`Deposit Action: No Token Found`);let M;if(!S){let t=w??await f.getFeeStructure(E,C,k,e.ExternalActionId.Transact);M=await m.calculateModifiedFeeStructure(E,j,-A[0],t),y.mergeWithFeeStructure(E,k,A,M)}let N={externalActionId:0n,externalAddress:o.addressToHexFormat(x),externalActionMetadata:`0x00`},[P,{inputUtxosArray:F,outputUtxosArray:I}]=await Promise.all([a.getRandomRelay(E,!0),b(a,E,k,A)]);if(!S&&!P)throw Error(i.transactionErrorCodes.RELAYER_NOT_AVAILABLE);let[{zkCallData:L,circomData:R,dimData:z},B]=await Promise.all([h.constructZkProof(a.merkleTreeHinkalByChain[E],F,I,a.userKeys,N.externalActionId,N.externalAddress,N.externalActionMetadata,a.generateProofRemotely,M??r.zeroFeeStructure,S?n.zeroAddress:P,E,void 0,void 0,S?O:void 0),p.buildCommitmentValidationData(E,a.userKeys,k,F)]),V=D&&S?void 0:await o.reorderZkCallData(D,L,z,R);if(S){if(!j)throw Error(`Withdraw Action: No Token Found`);return D?await v.transactCallDirectTron(a,E,A[0],j,L,R,z):await g.transactCallDirect(a,E,A[0],j,L,R,z)}return await _.transactCallRelayer(E,L,z,R,B,void 0,void 0,d.constructAdminData(T,E,k,A,O),V)};exports.hinkalWithdraw=x;