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