import { chainIds as e, networkRegistry as t } from "../../constants/chains.constants.mjs";
import { zeroAddress as n } from "../../constants/protocol.constants.mjs";
import { ContractType as r } from "../../types/ethereum-network.types.mjs";
import { caseInsensitiveEqual as i } from "../utils/caseInsensitive.utils.mjs";
import { getContractAddress as a } from "../utils/getContractAddress.mjs";
import { Logger as o } from "../../error-handling/logger.mjs";
import { createJsonRpcProvider as s } from "../utils/create-provider.mjs";
import { AccountActions as c } from "../../data-structures/AccountActions/AccountActions.mjs";
import "../utils/index.mjs";
import { ethers as l } from "ethers";
//#region libs/shared/common/src/functions/private-wallet/emporium.helpers.ts
var u = ({ contract: e, func: t, args: n, callDataString: r, invokeWallet: i, value: o }) => {
	let s = new Uint8Array(), c = r ? l.getBytesCopy(r) : new Uint8Array();
	if (typeof e == "object") {
		if (s = l.getBytesCopy(a(e)), r?.length && r?.length > 0) throw Error("If function and args are providedm no calldata is needed");
		t && n && !r && (c = l.getBytesCopy(e.interface.encodeFunctionData(t, n)));
	} else s = l.getBytesCopy(e);
	let u = l.getBytesCopy(new Uint8Array([+!!i])), d = l.zeroPadValue(l.toBeHex(o ?? 0n), 16), f = l.concat([
		s,
		u,
		d,
		c
	]);
	return l.hexlify(f);
}, d = async (e, t, r, i, a, o) => {
	let s = r ? await c.signTypedDataFromPrivateKey(e, r, {
		name: "Emporium",
		version: "1.0.0",
		chainId: e,
		verifyingContract: t
	}, { EmporiumSignature: [{
		name: "message",
		type: "uint256"
	}] }, { message: a }) : void 0, { v: u, r: d, s: f } = s ? l.Signature.from(s) : {
		v: 0,
		r: l.ZeroHash,
		s: l.ZeroHash
	};
	return l.AbiCoder.defaultAbiCoder().encode(["tuple(uint8 v,bytes32 r, bytes32 s, uint256 message, address signerAddress, bytes[] ops)"], [{
		v: u,
		r: d,
		s: f,
		message: a ?? 0n,
		signerAddress: o ?? n,
		ops: i
	}]);
}, f = (e, t, n, r) => u(e ? {
	contract: e,
	func: "transfer",
	args: [t, n],
	invokeWallet: r
} : {
	contract: t,
	value: n,
	invokeWallet: r
}), p = (e, t, o, s, c) => {
	let l = e.getContractWithFetcher(t, r.HinkalContract);
	return o.map((o, u) => f(i(o, n) ? void 0 : e.getContractWithFetcher(t, r.ERC20Contract, o), c ?? a(l), s[u], !0));
}, m = (e, t, n, r) => u({
	contract: e,
	func: "approve",
	args: [t, n],
	invokeWallet: r
}), h = (e, t, a, o, s, c, l, u) => {
	let d = [];
	for (let s = 0; s < a.length; s += 1) {
		let c = a[s], u = i(c, n) ? void 0 : e.getContractWithFetcher(t, r.ERC20Contract, c);
		d.push(f(u, l, o[s], !1));
	}
	if (u) for (let a = 0; a < s.length; a += 1) {
		let o = s[a];
		if (!i(o, n)) {
			let n = e.getContractWithFetcher(t, r.ERC20Contract, o);
			d.push(m(n, u, c[a], !0));
		}
	}
	return d;
}, g = (e, t, r, a, o, s, c, l) => {
	let d = [], f = [o], p = [s];
	l.nativeFee > 0n && !i(o, n) && (f.push(n), p.push(l.nativeFee));
	let m = h(e, t, f, p, [o], [c], r, a);
	d.push(...m);
	let g = l.nativeFee;
	return i(o, n) && (g += c), d.push(u({
		contract: a,
		callDataString: l.calldata,
		value: g > 0n ? g : void 0,
		invokeWallet: !0
	})), d;
}, _ = "0xef0100", v = async (e, t) => {
	try {
		let n = s(e), r = await n.getCode(t);
		if (r === "0x" || !r) return {
			delegated: !1,
			implementation: void 0,
			nonce: void 0
		};
		if (r.toLowerCase().startsWith(_) && r.length === 48) {
			let e = `0x${r.slice(8)}`, i = await n.getTransactionCount(t);
			return {
				delegated: !0,
				implementation: l.getAddress(e),
				nonce: i
			};
		} else throw Error("checkEOAWithDelegation: It is a contract");
	} catch (e) {
		throw o.error("checkEOAWithDelegation error", { error: e }), e;
	}
}, y = (e) => {
	let { hinkalWalletAddress: r } = t[e].contractData;
	if (!r || i(r, n)) throw Error("Hinkal Wallet Address is not set");
	return r;
}, b = async (t, n) => {
	if (t === e.localhost || !n) return;
	let { delegated: r, implementation: a } = await v(t, c.getSignerAddressFromPrivateKey(t, n)), o = y(t);
	if (!r || !i(a, o)) {
		let e = await c.authorizeDelegation(t, n, o);
		return {
			v: e.signature.v.toString(),
			r: e.signature.r,
			s: e.signature.s,
			nonce: e.nonce.toString(),
			address: o,
			chainId: String(t)
		};
	}
};
//#endregion
export { v as checkEOAWithDelegation, m as createApproveEmporiumOp, g as createLifiBridgeOps, p as createTransaferEmporiumOpsBatch, f as createTransferToEmporiumOp, u as emporiumOp, d as encodeEmporiumMetadata, h as generateFundAndApproveOps, b as getAuthorizationDataIfNeeded };
