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