const e=require(`../../constants/chains.constants.cjs`),t=require(`../../types/ethereum-network.types.cjs`),n=require(`../../types/external-action.types.cjs`),r=require(`../../types/admin.types.cjs`),i=require(`../../error-handling/error-codes.constants.cjs`),a=require(`../../functions/utils/caseInsensitive.utils.cjs`),o=require(`../../functions/utils/time.utils.cjs`),s=require(`../../functions/utils/addresses.cjs`),c=require(`../../functions/web3/events/getShieldedBalance.cjs`),l=require(`../../functions/pre-transaction/outputUtxoProcessing.cjs`);require(`../../error-handling/index.cjs`);const u=require(`../../functions/pre-transaction/constructAdminData.cjs`),d=require(`../AccountActions/AccountActions.cjs`),f=require(`../../functions/pre-transaction/recipientUtxoProcessing.cjs`),p=require(`../../functions/pre-transaction/getFeeStructure.cjs`),m=require(`../../functions/pre-transaction/getKycAndSignatureData.cjs`),h=require(`../../functions/pre-transaction/buildCommitmentValidationData.cjs`);require(`../../functions/pre-transaction/index.cjs`);const g=require(`../../functions/utils/involves-permit2-op.cjs`),_=require(`../../functions/utils/convertEmporiumOpToCallInfo.cjs`),v=require(`../../functions/snarkjs/constructEmporiumProof.cjs`),y=require(`../../functions/web3/functionCalls/transactCallRelayer.cjs`),b=require(`../../functions/pre-transaction/merge-with-fee-structure.cjs`),x=require(`../../functions/pre-transaction/merge-with-fee-structure-emporium.cjs`),S=require(`./handleAutoDepositBack.cjs`);var C=async(e,t,n,r,i)=>{let a=[...await c.addPaddingToUtxos(e,t,n,r)],s=[],u=o.getCurrentTimeInSeconds().toString();for(let t=0;t<n.length;t+=1){let{outputUtxos:n}=l.outputUtxoProcessing(e.userKeys,a[t],r[t],u);s.push(n)}return i&&f.recipientUtxoProcessing(i,s,r,u),{inputUtxosArray:a,outputUtxosArray:s}},w=async(o,c,l,f,w,T,E,D,O,k,A,j=!1,M,N,P)=>{if(P&&!s.isValidPrivateAddress(P.recipientInfo))throw Error(i.transactionErrorCodes.RECIPIENT_FORMAT_INCORRECT);if(E.length>0&&!D)throw Error(`Subaccount is required for private wallet transactions`);let F=l.map(e=>e.erc20TokenAddress),I=await o.getEthereumAddressByChain(c),L=D?d.AccountActions.getAddressFromSubAccount(c,D):void 0,{contractData:R}=e.networkRegistry[c],{emporiumAddress:z}=R;if(!z)throw Error(`No Emporium Address Provided`);let B=k??await p.getFeeStructure(c,O,F,n.ExternalActionId.Emporium,T.map(e=>_.convertEmporiumOpToCallInfo(e,L??``,c)));for(L?await x.mergeWithFeeStructureEmporium(o,c,L,T,F,f,B,E.find(e=>a.caseInsensitiveEqual(e.token.erc20TokenAddress,B.feeToken))?.amount??0n):b.mergeWithFeeStructure(c,F,f,B);w.length<F.length;)w.push(!1);let[{inputUtxosArray:V,outputUtxosArray:H},U,{patchAccessTokenMerkleTree:W,signatureData:G}]=await Promise.all([C(o,c,F,f,P),A??await o.getRandomRelay(c,!0),m.getKycAndSignatureData(o,c,F,f,I)]);if(!U)throw Error(i.transactionErrorCodes.RELAYER_NOT_AVAILABLE);let K=D?d.AccountActions.getPrivateKeyFromSubAccount(c,D):void 0,[{zkCallData:q,circomData:J,dimData:Y,authorizationData:X},Z]=await Promise.all([v.constructEmporiumProof(o.merkleTreeHinkalByChain[c],o.merkleTreeAccessTokenByChain[c],F,F.map(()=>0),V,H,o.userKeys,f.length,V.length>0?V[0].length:0,z,T,o.generateProofRemotely,U,c,w,void 0,K,W,void 0,B,o.getContractWithFetcher(c,t.ContractType.HinkalHelperContract),G),h.buildCommitmentValidationData(c,o.userKeys,F,V)]),Q=await y.transactCallRelayer(c,q,Y,J,Z,g.uniswapWorkaround(T),X,u.constructAdminData(N,c,E.map(e=>e.token.erc20TokenAddress),E.map(e=>e.amount),I,N===r.AdminTransactionType.PublicSwap?E.map(({token:e})=>e):void 0));return j&&D&&S.handleAutoDepositBack(o,c,L,D,B.feeToken,M,E,J),Q};exports.hinkalPrivateWallet=w;