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