const e=require(`../../constants/chains.constants.cjs`),t=require(`../../constants/protocol.constants.cjs`),n=require(`../../error-handling/error-codes.constants.cjs`);require(`../../constants/index.cjs`);const r=require(`../../data-structures/utxo/Utxo.cjs`),i=require(`./solanaMint.utils.cjs`),a=require(`./token-check.utils.cjs`);require(`../../error-handling/index.cjs`);const o=require(`../pre-transaction/sendV0Transaction.cjs`);require(`../pre-transaction/index.cjs`);const s=require(`./getUtxosFromReceipt.utils.cjs`),c=require(`./getUtxosFromReceiptSolana.cjs`),l=require(`./tx-confirmation.utils.cjs`),u=require(`./buildClaimableDepositPlan.cjs`);var d=10000n,f=e=>{if(e.isNewStyle&&e.H0)return e.H0[0];if(e.randomization!==void 0)return e.randomization},p=(e,t)=>{if(e.amount!==t.amount)return!1;let n=f(t),r=f(e);return n!==void 0&&r!==void 0&&n===r},m=async({chainId:t,fallbackRecipients:n,hinkal:a,senderAddress:s,tokenAddress:l,txHash:u})=>{let{hinkalIdl:d}=e.networkRegistry[t].contractData;if(!d)throw Error(`No IDL configured for Solana program on this network`);let f=a.getSolanaProgram(d);await f.provider.connection.confirmTransaction(u,`finalized`);let m=await o.fetchSolanaTransaction(f.provider.connection,u,`finalized`);if(!m)throw Error(`Could not fetch Solana transaction for signature ${u}`);let{compressedAddress:h}=i.formatMintAddress(l),g=[...c.getOnChainUtxosFromReceiptSolana(m,f,a.userKeys,h)],_=[];return n.forEach(({recipientIndex:e,recipientAddress:n,shieldedPrivateKey:i,claimableSignature:a,utxo:o})=>{let c=g.find(e=>p(e,o));if(!c)throw Error(`Could not match deposited UTXO for recipient index ${e}.`);g.splice(g.indexOf(c),1);let l=r.Utxo.createFrom(c,{nullifyingKey:i,spendingPublicKey:o.spendingPublicKey,isNewStyle:o.isNewStyle});l.getStealthAddress(),_.push({chainId:t,senderAddress:s,recipientAddress:n,claimableSignature:a,utxo:l})}),_},h=async({chainId:e,fallbackRecipients:t,hinkal:n,senderAddress:i,tokenAddress:a,txHash:o})=>{let c=await l.waitForEthereumTransactionConfirmation(e,o);if(!c)throw Error(`Could not fetch receipt for tx ${o}`);let u=[...s.getOnChainUtxosFromReceipt(c,n,e,a)],d=[];return t.forEach(({recipientIndex:t,recipientAddress:n,shieldedPrivateKey:a,claimableSignature:o,utxo:s})=>{let c=u.find(e=>p(e,s));if(!c)throw Error(`Could not match deposited UTXO for recipient index ${t}.`);u.splice(u.indexOf(c),1),d.push({chainId:e,senderAddress:i,recipientAddress:n,claimableSignature:o,utxo:r.Utxo.createFrom(c,{nullifyingKey:a,spendingPublicKey:s.spendingPublicKey,isNewStyle:s.isNewStyle})})}),d},g=async(r,i,o,s,c,l,f)=>{if(o.length===0)throw Error(`recipientAmounts must not be empty`);if(s.length!==o.length)throw Error(`recipientAddresses must have the same length as recipientAmounts`);if(l&&l.length!==o.length)throw Error(`recipientInfos must have the same length as recipientAmounts`);if(c<0n)throw Error(`nonce must be non-negative`);let p=a.validateAndGetChainId([i]),g=await r.getEthereumAddressByChain(p),_=i.erc20TokenAddress,v=o.reduce((e,t)=>{if(t<=0n)throw Error(n.transactionErrorCodes.RECIPIENT_AMOUNT_INVALID);return e+t},0n)*t.PAY_SEND_VARIABLE_RATE/d,{stealthAddressStructures:y,fallbackRecipients:b}=u.buildClaimableDepositPlan({userKeys:r.userKeys,recipientAmounts:o,recipientAddresses:s,recipientInfos:l,nonce:c,tokenAddress:_}),x;return x=v>0n?await r.prooflessDepositWithPublicFee(i,o,y,v,f):await r.prooflessDeposit(o.map(()=>i),o,y,f,!0),b.length===0?{pendingEnclaveUtxos:[],txHash:x}:{pendingEnclaveUtxos:e.isSolanaLike(p)?await m({chainId:p,fallbackRecipients:b,hinkal:r,senderAddress:g,tokenAddress:_,txHash:x}):await h({chainId:p,fallbackRecipients:b,hinkal:r,senderAddress:g,tokenAddress:_,txHash:x}),txHash:x}};exports.depositClaimableUtxos=g;