import { networkRegistry as e } from "../../constants/chains.constants.mjs";
import { ContractType as t } from "../../types/ethereum-network.types.mjs";
import { ExternalActionId as n } from "../../types/external-action.types.mjs";
import { AdminTransactionType as r } from "../../types/admin.types.mjs";
import { transactionErrorCodes as i } from "../../error-handling/error-codes.constants.mjs";
import { caseInsensitiveEqual as a } from "../../functions/utils/caseInsensitive.utils.mjs";
import { getCurrentTimeInSeconds as o } from "../../functions/utils/time.utils.mjs";
import { isValidPrivateAddress as s } from "../../functions/utils/addresses.mjs";
import { addPaddingToUtxos as c } from "../../functions/web3/events/getShieldedBalance.mjs";
import { outputUtxoProcessing as l } from "../../functions/pre-transaction/outputUtxoProcessing.mjs";
import "../../error-handling/index.mjs";
import { constructAdminData as u } from "../../functions/pre-transaction/constructAdminData.mjs";
import { AccountActions as d } from "../AccountActions/AccountActions.mjs";
import { recipientUtxoProcessing as f } from "../../functions/pre-transaction/recipientUtxoProcessing.mjs";
import { getFeeStructure as p } from "../../functions/pre-transaction/getFeeStructure.mjs";
import { getKycAndSignatureData as m } from "../../functions/pre-transaction/getKycAndSignatureData.mjs";
import { buildCommitmentValidationData as h } from "../../functions/pre-transaction/buildCommitmentValidationData.mjs";
import "../../functions/pre-transaction/index.mjs";
import { uniswapWorkaround as g } from "../../functions/utils/involves-permit2-op.mjs";
import { convertEmporiumOpToCallInfo as _ } from "../../functions/utils/convertEmporiumOpToCallInfo.mjs";
import { constructEmporiumProof as v } from "../../functions/snarkjs/constructEmporiumProof.mjs";
import { transactCallRelayer as y } from "../../functions/web3/functionCalls/transactCallRelayer.mjs";
import { mergeWithFeeStructure as b } from "../../functions/pre-transaction/merge-with-fee-structure.mjs";
import { mergeWithFeeStructureEmporium as x } from "../../functions/pre-transaction/merge-with-fee-structure-emporium.mjs";
import { handleAutoDepositBack as S } from "./handleAutoDepositBack.mjs";
//#region libs/shared/common/src/data-structures/Hinkal/hinkalPrivateWallet.ts
var C = async (e, t, n, r, i) => {
	let a = [...await c(e, t, n, r)], s = [], u = o().toString();
	for (let t = 0; t < n.length; t += 1) {
		let { outputUtxos: n } = l(e.userKeys, a[t], r[t], u);
		s.push(n);
	}
	return i && f(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(P.recipientInfo)) throw Error(i.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.getAddressFromSubAccount(c, D) : void 0, { contractData: R } = e[c], { emporiumAddress: z } = R;
	if (!z) throw Error("No Emporium Address Provided");
	let B = k ?? await p(c, O, F, n.Emporium, T.map((e) => _(e, L ?? "", c)));
	for (L ? await x(o, c, L, T, F, f, B, E.find((e) => a(e.token.erc20TokenAddress, B.feeToken))?.amount ?? 0n) : b(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(o, c, F, f, I)
	]);
	if (!U) throw Error(i.RELAYER_NOT_AVAILABLE);
	let K = D ? d.getPrivateKeyFromSubAccount(c, D) : void 0, [{ zkCallData: q, circomData: J, dimData: Y, authorizationData: X }, Z] = await Promise.all([v(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.HinkalHelperContract), G), h(c, o.userKeys, F, V)]), Q = await y(c, q, Y, J, Z, g(T), X, u(N, c, E.map((e) => e.token.erc20TokenAddress), E.map((e) => e.amount), I, N === r.PublicSwap ? E.map(({ token: e }) => e) : void 0));
	return j && D && S(o, c, L, D, B.feeToken, M, E, J), Q;
};
//#endregion
export { w as hinkalPrivateWallet };
