import { HINKAL_SWAP_VARIABLE_RATE as e, zeroAddress as t } from "../../constants/protocol.constants.mjs";
import { ContractType as n } from "../../types/ethereum-network.types.mjs";
import { ExternalActionId as r } from "../../types/external-action.types.mjs";
import { getERC20Registry as i } from "../../constants/token-data/ERC20Registry.mjs";
import "../../constants/token-data/index.mjs";
import { caseInsensitiveEqual as a } from "../utils/caseInsensitive.utils.mjs";
import { getContractAddress as o } from "../utils/getContractAddress.mjs";
import { Logger as s } from "../../error-handling/logger.mjs";
import { replaceAllInstances as c } from "../utils/string.utils.mjs";
import { NATIVE_TOKEN_WRAPPER_ADDRESSES as l, SWAP_FEE_TAKER_ADDRESSES as u, WRAPPER_TOKEN_EXCHANGE_ADDRESSES as d } from "../../constants/addresses.constants.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: c(r, m(e, t).slice(2), i.slice(2)),
	value: a,
	invokeWallet: o
}), v = (e, t) => {
	let n = i(t).find((e) => e.symbol === "USDT");
	return n ? a(e, n.erc20TokenAddress) : !1;
}, y = async (e, t, n, r, i, a) => {
	if (v(o(n), e)) try {
		await n.allowance(r, i) > 0n && t.push(f(n, i, 0n, a));
	} catch (e) {
		s.error("Error in nullifyUsdtApprovalIfNeeded", e);
	}
}, b = async (r, i, a, o, s, c, l, d) => {
	if (a !== t) {
		let t = u[i], d = r.getContractWithFetcher(i, n.ERC20Contract, a), m = await d.balanceOf(o);
		await y(i, c, d, o, t, l), c.push(f(d, t, 2n ** 256n - 1n, l));
		let g = new h.Contract(t, ["function takeSwapFee(address token, uint256 beforeBalance, uint256 variableRate, address relay) external payable"]), _ = s;
		c.push(p({
			contract: g,
			func: "takeSwapFee",
			args: [
				a,
				m,
				e,
				_
			],
			invokeWallet: l
		}));
	} else {
		let t = s;
		c.push(p({
			contract: t,
			value: d * e / 10000n,
			invokeWallet: l
		}));
	}
}, x = async (e, i, a, o, s, c, u, m, v, x, S, C, w = !1) => {
	let T = u < 0n ? -1n * u : u, E = [], D = l[i], O = !1, k;
	if (a === r.OneInch || a === r.Lifi) {
		if (c !== t) {
			let t = e.getContractWithFetcher(i, n.ERC20Contract, c);
			E.push(f(t, o, T, w));
		}
		let l = 0n;
		return c === t && (l += T), m && v === t && m < 0n && (l += -1n * m), k = _(i, a, o, s, S, l > 0n ? l : void 0, w), E.push(k), (a === r.OneInch || x && a === r.Lifi) && v && m !== void 0 && await b(e, i, v, S, C, E, w, m), E;
	}
	if (!v || !m) throw Error("There should be output token for UniSwap and Odos");
	if (c === t) {
		let t = e.getContractWithFetcher(i, n.ERC20Contract, D);
		E.push(p({
			contract: t,
			func: "deposit",
			value: T,
			invokeWallet: w
		})), c = D;
	}
	let A = 0n;
	if (v === t && (v = D, O = !0, A = await e.getContractWithFetcher(i, n.ERC20Contract, D).balanceOf(S)), c !== t) {
		let t = e.getContractWithFetcher(i, n.ERC20Contract, c);
		await y(i, E, t, S, o, w), E.push(f(t, o, T, w));
	}
	if (k = a === r.Uniswap ? g(o, c, T, v, S, s, w) : _(i, a, o, s, S, void 0, w), E.push(k), await b(e, i, v, S, C, E, w, m), O) {
		let t = d[i], r = e.getContractWithFetcher(i, n.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 };
