import { ContractType as e } from "../../types/ethereum-network.types.mjs";
import { networkRegistry as t } from "../../constants/chains.constants.mjs";
import "../../types/index.mjs";
import { caseInsensitiveEqual as n } from "./caseInsensitive.utils.mjs";
import { toBigInt as r } from "./amounts.utils.mjs";
import { UserKeys as i } from "../../data-structures/crypto-keys/keys.mjs";
import { decodeUtxo as a } from "../../data-structures/crypto-keys/decodeUTXO.mjs";
import { decryptUtxo as o } from "../../data-structures/crypto-keys/encryptDecryptUtxo.mjs";
import { getContractAddress as s } from "./getContractAddress.mjs";
import { ethers as c } from "ethers";
//#region libs/shared/common/src/functions/utils/getUtxosFromReceipt.utils.ts
var l = (t, i, o, c) => {
	let l = i.getContractWithFetcher(o, e.HinkalContract), u = l.interface, d = i.userKeys.getShieldedPrivateKey(), f = s(l);
	return t.logs.filter((e) => n(e.address, f)).map((e) => {
		try {
			let t = u.parseLog(e);
			if (t?.name === "NewCommitment") {
				let { encryptedOutput: e, index: i } = t.args;
				if (r(i) < 0n) {
					let t = a(e, d, o);
					if (n(t.erc20TokenAddress, c)) return t;
				}
			}
		} catch {
			return null;
		}
		return null;
	}).filter((e) => e !== null);
}, u = (t, a, l, u, d) => {
	let f = a.getContractWithFetcher(l, e.HinkalContract), p = f.interface;
	return t.logs.filter((e) => n(e.address, s(f))).map((e) => {
		try {
			let t = p.parseLog(e);
			if (t?.name === "NewCommitment") {
				let { encryptedOutput: e, index: a } = t.args;
				if (r(a) >= 0n) {
					let t = new i(void 0, d);
					try {
						let r = o(c.getBytes(e), t);
						if (n(r.erc20TokenAddress, u)) return r;
					} catch {
						return null;
					}
				}
			}
			return null;
		} catch {
			return null;
		}
	}).filter((e) => e !== null);
}, d = (e, i) => {
	if (i.status !== 1) throw Error(`Transaction failed: ${i.hash}`);
	let { contractData: a } = t[e], { hinkalAddress: o, hinkalABI: s } = a;
	if (!o || !s) throw Error(`Hinkal contract data not found for chainId ${e}`);
	let l = new c.Interface(s);
	return i.logs.filter((e) => n(e.address, o)).map((e) => {
		try {
			let t = l.parseLog(e);
			if (t?.name !== "NewCommitment") return null;
			let n = t.args.commitment;
			return n ? r(n).toString() : null;
		} catch {
			return null;
		}
	}).filter((e) => e !== null);
};
//#endregion
export { l as getOnChainUtxosFromReceipt, d as getOutputCommitmentsFromEvmReceipt, u as getUtxosFromReceipt };
