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