import { ContractType as e } from "../../types/ethereum-network.types.mjs";
import { ExternalActionId as t } from "../../types/external-action.types.mjs";
import { HINKAL_SWAP_VARIABLE_RATE as n, zeroAddress as r } from "../../constants/protocol.constants.mjs";
import { caseInsensitiveEqual as i } from "../utils/caseInsensitive.utils.mjs";
import { getERC20Registry as a } from "../../constants/token-data/ERC20Registry.mjs";
import "../../constants/token-data/index.mjs";
import { NATIVE_TOKEN_WRAPPER_ADDRESSES as o, SWAP_FEE_TAKER_ADDRESSES as s, WRAPPER_TOKEN_EXCHANGE_ADDRESSES as c } from "../../constants/addresses.constants.mjs";
import { Logger as l } from "../../error-handling/logger.mjs";
import { replaceAllInstances as u } from "../utils/string.utils.mjs";
import { getContractAddress as d } from "../utils/getContractAddress.mjs";
import { createApproveEmporiumOp as f, emporiumOp as p } from "./emporium.helpers.mjs";
import { getExternalSwapAddress as m } from "../pre-transaction/getExternalSwapAddress.mjs";
import { ethers as h } from "ethers";
//#region libs/shared/common/src/functions/private-wallet/emporium.swap.helpers.ts
var g = (e, t, n, r, i, a, o = !1) => {
	let s = {
		tokenIn: t,
		tokenOut: r,
		fee: BigInt(a),
		recipient: i,
		amountIn: n,
		amountOutMinimum: 0,
		sqrtPriceLimitX96: 0
	};
	return p({
		contract: new h.Contract(e, ["function exactInputSingle(tuple(address tokenIn,address tokenOut,uint24 fee,address recipient,uint256 amountIn,uint256 amountOutMinimum,uint160 sqrtPriceLimitX96)) external payable returns (uint256 amountOut)"]),
		func: "exactInputSingle",
		args: [s],
		invokeWallet: o
	});
}, _ = (e, t, n, r, i, a, o = !1) => p({
	contract: n,
	callDataString: u(r, m(e, t).slice(2), i.slice(2)),
	value: a,
	invokeWallet: o
}), v = (e, t) => {
	let n = a(t).find((e) => e.symbol === "USDT");
	return n ? i(e, n.erc20TokenAddress) : !1;
}, y = async (e, t, n, r, i, a) => {
	if (v(d(n), e)) try {
		await n.allowance(r, i) > 0n && t.push(f(n, i, 0n, a));
	} catch (e) {
		l.error("Error in nullifyUsdtApprovalIfNeeded", e);
	}
}, b = async (t, i, a, o, c, l, u, d) => {
	if (a !== r) {
		let r = s[i], d = t.getContractWithFetcher(i, e.ERC20Contract, a), m = await d.balanceOf(o);
		await y(i, l, d, o, r, u), l.push(f(d, r, 2n ** 256n - 1n, u));
		let g = new h.Contract(r, ["function takeSwapFee(address token, uint256 beforeBalance, uint256 variableRate, address relay) external payable"]), _ = c;
		l.push(p({
			contract: g,
			func: "takeSwapFee",
			args: [
				a,
				m,
				n,
				_
			],
			invokeWallet: u
		}));
	} else {
		let e = c;
		l.push(p({
			contract: e,
			value: d * n / 10000n,
			invokeWallet: u
		}));
	}
}, x = async (n, i, a, s, l, u, d, m, v, x, S, C, w = !1) => {
	let T = d < 0n ? -1n * d : d, E = [], D = o[i], O = !1, k;
	if (a === t.OneInch || a === t.Lifi) {
		if (u !== r) {
			let t = n.getContractWithFetcher(i, e.ERC20Contract, u);
			E.push(f(t, s, T, w));
		}
		let o = 0n;
		return u === r && (o += T), m && v === r && m < 0n && (o += -1n * m), k = _(i, a, s, l, S, o > 0n ? o : void 0, w), E.push(k), (a === t.OneInch || x && a === t.Lifi) && v && m !== void 0 && await b(n, i, v, S, C, E, w, m), E;
	}
	if (!v || !m) throw Error("There should be output token for UniSwap and Odos");
	if (u === r) {
		let t = n.getContractWithFetcher(i, e.ERC20Contract, D);
		E.push(p({
			contract: t,
			func: "deposit",
			value: T,
			invokeWallet: w
		})), u = D;
	}
	let A = 0n;
	if (v === r && (v = D, O = !0, A = await n.getContractWithFetcher(i, e.ERC20Contract, D).balanceOf(S)), u !== r) {
		let t = n.getContractWithFetcher(i, e.ERC20Contract, u);
		await y(i, E, t, S, s, w), E.push(f(t, s, T, w));
	}
	if (k = a === t.Uniswap ? g(s, u, T, v, S, l, w) : _(i, a, s, l, S, void 0, w), E.push(k), await b(n, i, v, S, C, E, w, m), O) {
		let t = c[i], r = n.getContractWithFetcher(i, e.ERC20Contract, D);
		E.push(f(r, t, 2n ** 256n - 1n, w));
		let a = new h.Contract(t, ["function withdrawBalanceDifference(uint256 beforeBalance) external"]);
		E.push(p({
			contract: a,
			func: "withdrawBalanceDifference",
			args: [A],
			invokeWallet: w
		}));
	}
	return E;
};
//#endregion
export { g as createSwapToEmporiumOpForUniswap, x as createSwapToEmporiumOps };
