require(`../../../../_virtual/_rolldown/runtime.cjs`);const e=require(`../../constants/chains.constants.cjs`);require(`../../constants/protocol.constants.cjs`);const t=require(`../../types/external-action.types.cjs`);require(`../../types/index.cjs`);const n=require(`../../error-handling/error-codes.constants.cjs`),r=require(`../../functions/web3/etherFunctions.cjs`),i=require(`../../functions/utils/time.utils.cjs`),a=require(`../utxo/Utxo.cjs`),o=require(`../../functions/utils/token-check.utils.cjs`),s=require(`../../functions/web3/events/getShieldedBalance.cjs`),c=require(`../../functions/pre-transaction/outputUtxoProcessing.cjs`),l=require(`../../functions/snarkjs/common.snarkjs.cjs`),u=require(`../../functions/utils/fees.utils.cjs`),d=require(`../../functions/pre-transaction/constructAdminData.cjs`),f=require(`../../functions/pre-transaction/ensureAmountChanges.cjs`),p=require(`../../functions/pre-transaction/getFeeStructure.cjs`),m=require(`../../functions/pre-transaction/shouldPatchAccessTokenMerkleTree.cjs`),h=require(`../../functions/pre-transaction/buildCommitmentValidationData.cjs`);require(`../../functions/pre-transaction/index.cjs`),require(`../../constants/index.cjs`);const g=require(`../../functions/snarkjs/constructSolanaZkProof.cjs`),_=require(`../../functions/web3/functionCalls/transactCallRelayer.cjs`);let v=require(`ethers`);var y=async(e,t,n,r)=>{let a=[...await s.addPaddingToUtxos(e,t,n,r,6)][0],o=i.getCurrentTimeInSeconds().toString(),{outputUtxos:l}=c.outputUtxoProcessing(e.userKeys,a,r[0],o);return{inputUtxos:a,outputUtxos:l}},b=async(i,s,c,b,x,S,C)=>{let w=o.validateAndGetChainId(s),T=s.map(e=>e.erc20TokenAddress),{originalDeployer:E,hinkalIdl:D}=e.networkRegistry[w].contractData;if(!E||!D)throw Error(`missing data`);let{userKeys:O,merkleTreeAccessTokenByChain:k,merkleTreeHinkalByChain:A}=i,j=S??await p.getFeeStructure(w,x,T,t.ExternalActionId.Transact),M=u.calculateTotalFee(-c[0],j),N={feeToken:j.feeToken,flatFee:M,variableRate:0n};c[0]-=N.flatFee;let[{patchAccessTokenMerkleTree:P,kycRequired:F,hasAccessToken:I},L,R,{inputUtxos:z,outputUtxos:B}]=await Promise.all([m.shouldPatchAccessTokenMerkleTree(i,w,T,c),i.getEthereumAddressByChain(w),i.getRandomRelay(w,!0),y(i,w,T,c)]);if(!R)throw Error(n.transactionErrorCodes.RELAYER_NOT_AVAILABLE);if(T.length>1)throw Error(`Solana Withdraw: Only one mint address is supported`);let V=a.Utxo.findCorrectRandomization(r.randomBigInt(31),i.userKeys.getShieldedPrivateKey()),H={tokenNumber:T.length,nullifierAmount:z.length,outputAmount:B.length},U=i.getSolanaProgram(D),W=l.calcEncryptedOutputs([B])[0][0],G=Array.from(v.ethers.getBytes(W));f.ensureAmountChanges([z],[B],c);let[{proofAArr:K,proofBArr:q,proofCArr:J,publicInputsArr:Y},X]=await Promise.all([g.constructSolanaZkProof(i.generateProofRemotely,E,A[w],k[w],O,U,[T[0]],[z],[B],V,N.flatFee,N.variableRate,b,R,H,[G],w,P),h.buildCommitmentValidationData(w,O,T,[z])]),Z={recipient:b,mint:T[0]===`11111111111111111111111111111111`?void 0:T[0]},Q={proofAArr:K,proofBArr:q,proofCArr:J,publicInputsArr:Y,encryptedOutputs:[G],relayerFee:N.flatFee.toString(),dimensions:H},$=d.constructAdminData(C,w,T,c,await i.getEthereumAddress());return _.solanaTransactCallRelayer({chainId:w,relayAddress:R,functionName:`transact`,args:Q,accounts:Z,commitmentValidationData:X,solanaAccessTokenData:!I&&F?{ethereumAddress:L,accessKey:O.getAccessKey()}:void 0,adminData:$})};exports.hinkalSolanaWithdraw=b;