require(`../../../../_virtual/_rolldown/runtime.cjs`);const e=require(`../../constants/chains.constants.cjs`),t=require(`../../constants/protocol.constants.cjs`),n=require(`../../types/external-action.types.cjs`),r=require(`../../types/admin.types.cjs`);require(`../../types/index.cjs`);const i=require(`../../error-handling/error-codes.constants.cjs`),a=require(`../../functions/web3/etherFunctions.cjs`),o=require(`../crypto-keys/keys.cjs`),s=require(`../../functions/utils/time.utils.cjs`),c=require(`../utxo/Utxo.cjs`),l=require(`../../functions/utils/token-check.utils.cjs`),u=require(`../../functions/snarkjs/common.snarkjs.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/snarkjs/constructSolanaZkProof.cjs`),h=require(`../../functions/web3/functionCalls/transactCallRelayer.cjs`);let g=require(`ethers`);var _=async(_,v,y,b,x)=>{let S=l.validateAndGetChainId([v]),C=v.erc20TokenAddress;if(y.getTokenAddress(S)!==C)throw Error(`Off-chain UTXO token mismatch`);if(!y.shieldedPrivateKey)throw Error(`Claimable UTXO shieldedPrivateKey is missing`);let{originalDeployer:w,hinkalIdl:T}=e.networkRegistry[S].contractData;if(!w||!T)throw Error(`missing data`);let E=new o.UserKeys(void 0,y.shieldedPrivateKey),D=new c.Utxo({amount:y.amount,erc20TokenAddress:y.erc20TokenAddress,mintAddress:y.mintAddress,timeStamp:y.timeStamp,tokenId:y.tokenId,randomization:y.randomization,shieldedPrivateKey:E.getShieldedPrivateKey()}),O=b??await f.getFeeStructure(S,C,[C],n.ExternalActionId.Transact,void 0,void 0,{mintTo:C,nullifierCount:1});if(O.feeToken!==C)throw Error(`Claim fee token mismatch: fee must be paid from claimed UTXO token`);let k=O.variableRate>0n?O.variableRate:t.HINKAL_PRIVATE_SEND_VARIABLE_RATE;if(D.amount<=O.flatFee)throw Error(i.transactionErrorCodes.INSUFFICIENT_FUNDS_TO_TRANSACT);let A=D.amount-O.flatFee,j=A*k/10000n,M=A-j;if(M<=0n)throw Error(i.transactionErrorCodes.RECIPIENT_AMOUNT_INVALID);let N=O.flatFee+j,[P,F,I]=_.getRecipientInfo().split(`,`);if(!P||!F||!I)throw Error(`Invalid recipient info`);let L=s.getCurrentTimeInSeconds().toString(),R=new c.Utxo({amount:0n,erc20TokenAddress:D.erc20TokenAddress,mintAddress:D.mintAddress,tokenId:D.tokenId,shieldedPrivateKey:E.getShieldedPrivateKey()}),z=new c.Utxo({amount:0n,erc20TokenAddress:D.erc20TokenAddress,mintAddress:D.mintAddress,tokenId:D.tokenId,shieldedPrivateKey:E.getShieldedPrivateKey(),timeStamp:L}),B=new c.Utxo({amount:M,erc20TokenAddress:D.erc20TokenAddress,mintAddress:D.mintAddress,tokenId:D.tokenId,timeStamp:L,randomization:BigInt(P),stealthAddress:F,encryptionKey:I}),V=[D,R],H=[z,B],U=await _.getRandomRelay(S,!0);if(!U)throw Error(i.transactionErrorCodes.RELAYER_NOT_AVAILABLE);let W=c.Utxo.findCorrectRandomization(a.randomBigInt(31),E.getShieldedPrivateKey()),G={tokenNumber:1,nullifierAmount:V.length,outputAmount:H.length},K=u.calcEncryptedOutputs([H])[0].map(e=>Array.from(g.ethers.getBytes(e))),q=_.getSolanaProgram(T),[{proofAArr:J,proofBArr:Y,proofCArr:X,publicInputsArr:Z},Q]=await Promise.all([m.constructSolanaZkProof(_.generateProofRemotely,w,_.merkleTreeHinkalByChain[S],_.merkleTreeAccessTokenByChain[S],E,q,[C],[V],[H],W,N,0n,t.solanaNativeAddress,U,G,K,S),p.buildCommitmentValidationData(S,E,[C],[V])]),$={recipient:U,mint:C===`11111111111111111111111111111111`?void 0:C};return h.solanaTransactCallRelayer({chainId:S,relayAddress:U,functionName:`transfer`,args:{proofAArr:J,proofBArr:Y,proofCArr:X,publicInputsArr:Z,encryptedOutputs:K,relayerFee:N.toString(),dimensions:G},accounts:$,adminData:d.constructAdminData(x??r.AdminTransactionType.ClaimUtxo,S,[C],[-A],await _.getEthereumAddress()),commitmentValidationData:Q})};exports.hinkalSolanaClaimUtxo=_;