import { c as stringEnum, i as jsonResult, o as readNumberParam, r as errorResult, s as readStringParam, u as Type } from "../../tool-helpers-CVAcqiCy.mjs";
import { fetchDexScreener } from "../services/dexscreener-service.mjs";
//#region extensions/crypto/src/tools/market-intel.ts
/**
* Market Intel Tool — market data, trending analysis, whale watching
* 
* Inspired by Lemon's MarketIntel. Ingests data from DexScreener,
* CoinGecko, and on-chain events to surface market movements.
*/
const MarketIntelSchema = Type.Object({
	action: stringEnum([
		"trending",
		"new_pairs",
		"whale_watch",
		"analysis",
		"leaderboard"
	], { description: "trending: hot tokens. new_pairs: recently created pools. whale_watch: large trades. analysis: token deep-dive. leaderboard: top Clawnch agents." }),
	token: Type.Optional(Type.String({ description: "Token address or symbol (for analysis action)" })),
	chain: Type.Optional(Type.String({ description: "Chain to query (default: base)" })),
	limit: Type.Optional(Type.Number({ description: "Number of results to return (default: 10)" }))
});
function createMarketIntelTool() {
	return {
		name: "market_intel",
		label: "Market Intel",
		ownerOnly: false,
		description: "Real-time market intelligence — trending tokens, new pairs, whale movements, token analysis, and Clawnch agent leaderboard. Uses DexScreener, CoinGecko, and Clawnch platform data.",
		parameters: MarketIntelSchema,
		execute: async (_toolCallId, args) => {
			const params = args;
			const action = readStringParam(params, "action", { required: true });
			switch (action) {
				case "trending": return handleTrending(params);
				case "new_pairs": return handleNewPairs(params);
				case "whale_watch": return handleWhaleWatch(params);
				case "analysis": return handleAnalysis(params);
				case "leaderboard": return handleLeaderboard(params);
				default: return errorResult(`Unknown action: ${action}`);
			}
		}
	};
}
async function handleTrending(params) {
	const chain = readStringParam(params, "chain") || "base";
	const limit = readNumberParam(params, "limit") ?? 10;
	try {
		const trending = (await fetchDexScreener("/token-boosts/top/v1") ?? []).filter((t) => t.chainId === chain).slice(0, limit).map((t, i) => ({
			rank: i + 1,
			address: t.tokenAddress,
			chain: t.chainId,
			url: t.url,
			boostAmount: t.totalAmount
		}));
		const addresses = trending.map((t) => t.address).filter(Boolean);
		let profiles = [];
		if (addresses.length > 0) try {
			const profileData = await fetchDexScreener(`/tokens/v1/${chain}/${addresses.slice(0, 5).join(",")}`);
			profiles = Array.isArray(profileData) ? profileData : profileData?.pairs ?? [];
		} catch {}
		return jsonResult({
			chain,
			trending,
			count: trending.length,
			profiles: profiles.slice(0, 5).map((p) => ({
				symbol: p.baseToken?.symbol,
				name: p.baseToken?.name,
				priceUsd: p.priceUsd,
				change24h: p.priceChange?.h24,
				volume24h: p.volume?.h24,
				liquidity: p.liquidity?.usd
			}))
		});
	} catch (err) {
		return errorResult(`Trending lookup failed: ${err instanceof Error ? err.message : String(err)}`);
	}
}
async function handleNewPairs(params) {
	const chain = readStringParam(params, "chain") || "base";
	const limit = readNumberParam(params, "limit") ?? 10;
	try {
		const pairs = ((await fetchDexScreener(`/latest/dex/pairs/${chain}`))?.pairs ?? []).slice(0, limit);
		return jsonResult({
			chain,
			newPairs: pairs.map((p) => ({
				pairAddress: p.pairAddress,
				baseToken: {
					symbol: p.baseToken?.symbol,
					name: p.baseToken?.name,
					address: p.baseToken?.address
				},
				quoteToken: p.quoteToken?.symbol,
				dexId: p.dexId,
				priceUsd: p.priceUsd,
				liquidity: p.liquidity?.usd,
				volume24h: p.volume?.h24,
				pairCreatedAt: p.pairCreatedAt,
				url: p.url
			})),
			count: pairs.length
		});
	} catch (err) {
		return errorResult(`New pairs lookup failed: ${err instanceof Error ? err.message : String(err)}`);
	}
}
async function handleWhaleWatch(params) {
	const token = readStringParam(params, "token");
	const chain = readStringParam(params, "chain") || "base";
	try {
		if (token) {
			const data = await fetchDexScreener(token.startsWith("0x") ? `/tokens/v1/${chain}/${token}` : `/latest/dex/search?q=${encodeURIComponent(token)}`);
			const pairs = data?.pairs ?? (Array.isArray(data) ? data : []);
			const topPair = pairs.find((p) => p.chainId === chain) ?? pairs[0];
			if (!topPair) return jsonResult({
				found: false,
				token,
				chain
			});
			return jsonResult({
				token: topPair.baseToken?.symbol,
				address: topPair.baseToken?.address,
				chain,
				volume: {
					h1: topPair.volume?.h1,
					h6: topPair.volume?.h6,
					h24: topPair.volume?.h24
				},
				txns: {
					h1: topPair.txns?.h1,
					h6: topPair.txns?.h6,
					h24: topPair.txns?.h24
				},
				priceChange: {
					h1: topPair.priceChange?.h1,
					h6: topPair.priceChange?.h6,
					h24: topPair.priceChange?.h24
				},
				note: "Volume spikes and transaction counts can indicate whale activity. For detailed on-chain whale tracking, integrate with Herd Intelligence."
			});
		}
		return jsonResult({
			chain,
			note: "Provide a token address or symbol for whale activity analysis. General whale watching requires on-chain event streaming (future feature)."
		});
	} catch (err) {
		return errorResult(`Whale watch failed: ${err instanceof Error ? err.message : String(err)}`);
	}
}
async function handleAnalysis(params) {
	const token = readStringParam(params, "token", { required: true });
	const chain = readStringParam(params, "chain") || "base";
	try {
		const dexData = await fetchDexScreener(token.startsWith("0x") ? `/tokens/v1/${chain}/${token}` : `/latest/dex/search?q=${encodeURIComponent(token)}`);
		const pairs = dexData?.pairs ?? (Array.isArray(dexData) ? dexData : []);
		const mainPair = pairs.find((p) => p.chainId === chain) ?? pairs[0];
		if (!mainPair) return jsonResult({
			found: false,
			token,
			chain
		});
		let clawnchData = null;
		try {
			const { ClawnchClient } = await import("../../dist-CXAY4yCt.mjs");
			clawnchData = await new ClawnchClient({ baseUrl: process.env.CLAWNCHER_API_URL || "https://clawn.ch" }).getTokenAnalytics(mainPair.baseToken?.address);
		} catch {}
		return jsonResult({
			token: {
				symbol: mainPair.baseToken?.symbol,
				name: mainPair.baseToken?.name,
				address: mainPair.baseToken?.address
			},
			chain: mainPair.chainId,
			price: {
				usd: mainPair.priceUsd,
				native: mainPair.priceNative
			},
			change: {
				m5: mainPair.priceChange?.m5,
				h1: mainPair.priceChange?.h1,
				h6: mainPair.priceChange?.h6,
				h24: mainPair.priceChange?.h24
			},
			volume: {
				h1: mainPair.volume?.h1,
				h6: mainPair.volume?.h6,
				h24: mainPair.volume?.h24
			},
			transactions: mainPair.txns?.h24,
			liquidity: mainPair.liquidity?.usd,
			marketCap: mainPair.marketCap ?? mainPair.fdv,
			dex: mainPair.dexId,
			pairCreated: mainPair.pairCreatedAt,
			url: mainPair.url,
			clawnch: clawnchData ?? void 0
		});
	} catch (err) {
		return errorResult(`Analysis failed: ${err instanceof Error ? err.message : String(err)}`);
	}
}
async function handleLeaderboard(params) {
	const limit = readNumberParam(params, "limit") ?? 20;
	try {
		const { ClawnchClient } = await import("../../dist-CXAY4yCt.mjs");
		return jsonResult({
			leaderboard: await new ClawnchClient({ baseUrl: process.env.CLAWNCHER_API_URL || "https://clawn.ch" }).getLeaderboard("market_cap", limit),
			note: "Ranked by total market cap of all tokens launched by each agent."
		});
	} catch (err) {
		try {
			const { ClawnchClient } = await import("../../dist-CXAY4yCt.mjs");
			return jsonResult({
				stats: await new ClawnchClient({ baseUrl: process.env.CLAWNCHER_API_URL || "https://clawn.ch" }).getStats(),
				note: "Full leaderboard unavailable. Showing platform stats instead."
			});
		} catch (err2) {
			return errorResult(`Leaderboard failed: ${err instanceof Error ? err.message : String(err)}`);
		}
	}
}
//#endregion
export { createMarketIntelTool };

//# sourceMappingURL=market-intel.mjs.map