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 { getTransactionHistory, getWalletState } from "../services/walletconnect-service.mjs";
import { getEthPrice, getPrice } from "../services/price-service.mjs";
import { validateLaunch, validateSwap } from "../services/safety-service.mjs";
//#region extensions/crypto/src/tools/crypto-workflow.ts
/**
* Crypto Workflow Tool — orchestrates multi-tool pipelines.
*
* Named workflows chain existing tool logic into safe, complete operations.
* Each workflow runs pre-flight checks, executes the core action, and
* performs follow-up steps (monitoring, social, order management).
*
* This tool exists because LLMs can forget steps in a multi-tool sequence.
* The workflow guarantees all steps happen, in order, with safety checks.
*/
const CryptoWorkflowSchema = Type.Object({
	workflow: stringEnum([
		"safe_swap",
		"launch_and_promote",
		"check_orders",
		"portfolio_snapshot"
	], { description: "safe_swap: price check + audit + balance check + swap + set stop-loss. launch_and_promote: balance check + deploy + generate tweet text. check_orders: auto-fetch prices and check all order triggers. portfolio_snapshot: balance + positions + unrealized PnL." }),
	token_in: Type.Optional(Type.String({ description: "Token to sell (for safe_swap)" })),
	token_out: Type.Optional(Type.String({ description: "Token to buy (for safe_swap)" })),
	amount: Type.Optional(Type.String({ description: "Amount to sell (for safe_swap)" })),
	slippage: Type.Optional(Type.Number({ description: "Slippage % (for safe_swap, default 1.0)" })),
	stop_loss_pct: Type.Optional(Type.Number({ description: "Auto stop-loss % below entry (for safe_swap, default: none)" })),
	name: Type.Optional(Type.String({ description: "Token name (for launch_and_promote)" })),
	symbol: Type.Optional(Type.String({ description: "Token symbol (for launch_and_promote)" })),
	description: Type.Optional(Type.String({ description: "Token description (for launch_and_promote)" })),
	dev_buy_eth: Type.Optional(Type.String({ description: "Dev buy ETH amount (for launch_and_promote)" }))
});
function createCryptoWorkflowTool() {
	return {
		name: "crypto_workflow",
		label: "Crypto Workflow",
		ownerOnly: true,
		description: "Multi-step crypto workflows that chain tools together with safety checks. Use \"safe_swap\" for audited token swaps, \"launch_and_promote\" for token launches with auto-generated tweet text, \"check_orders\" to auto-check all order triggers with live prices, \"portfolio_snapshot\" for a full portfolio view.",
		parameters: CryptoWorkflowSchema,
		execute: async (_toolCallId, args) => {
			const params = args;
			const workflow = readStringParam(params, "workflow", { required: true });
			switch (workflow) {
				case "safe_swap": return handleSafeSwap(params);
				case "launch_and_promote": return handleLaunchAndPromote(params);
				case "check_orders": return handleCheckOrders();
				case "portfolio_snapshot": return handlePortfolioSnapshot();
				default: return errorResult(`Unknown workflow: ${workflow}`);
			}
		}
	};
}
async function handleSafeSwap(params) {
	const tokenIn = readStringParam(params, "token_in", { required: true });
	const tokenOut = readStringParam(params, "token_out", { required: true });
	const amount = readStringParam(params, "amount", { required: true });
	const slippage = readNumberParam(params, "slippage") ?? 1;
	const stopLossPct = readNumberParam(params, "stop_loss_pct");
	const steps = [];
	const state = getWalletState();
	if (!state.connected) return errorResult("No wallet connected. Use clawnchconnect tool first.");
	steps.push({
		step: "wallet_check",
		status: "ok",
		data: { address: state.address }
	});
	let priceIn = null;
	let priceOut = null;
	try {
		[priceIn, priceOut] = await Promise.all([getPrice(tokenIn).catch(() => null), getPrice(tokenOut).catch(() => null)]);
		steps.push({
			step: "price_lookup",
			status: "ok",
			data: {
				tokenIn: priceIn ? {
					symbol: priceIn.symbol,
					priceUsd: priceIn.priceUsd
				} : "unknown",
				tokenOut: priceOut ? {
					symbol: priceOut.symbol,
					priceUsd: priceOut.priceUsd
				} : "unknown"
			}
		});
	} catch {
		steps.push({
			step: "price_lookup",
			status: "warning",
			data: "Price lookup failed"
		});
	}
	const safety = await validateSwap({
		tokenIn,
		tokenOut,
		amountEth: parseFloat(amount)
	});
	steps.push({
		step: "safety_check",
		status: safety.safe ? "ok" : "blocked",
		data: {
			safe: safety.safe,
			warnings: safety.warnings,
			blockers: safety.blockers
		}
	});
	if (!safety.safe) return jsonResult({
		workflow: "safe_swap",
		status: "blocked",
		steps,
		message: "Swap blocked by safety checks. Review blockers above."
	});
	const swapInstruction = {
		tool: "defi_swap",
		action: "execute",
		params: {
			token_in: tokenIn,
			token_out: tokenOut,
			amount,
			slippage
		}
	};
	steps.push({
		step: "swap_ready",
		status: "ready",
		data: swapInstruction
	});
	let stopLossInstruction = null;
	if (stopLossPct && priceOut) {
		stopLossInstruction = {
			tool: "manage_orders",
			action: "create",
			params: {
				type: "stop_loss",
				token: tokenOut,
				trigger_price: (priceOut.priceEth * (1 - stopLossPct / 100)).toFixed(8),
				amount_pct: 100,
				description: `Auto stop-loss at -${stopLossPct}% from entry`
			}
		};
		steps.push({
			step: "stop_loss_prepared",
			status: "ready",
			data: stopLossInstruction
		});
	}
	return jsonResult({
		workflow: "safe_swap",
		status: "ready",
		steps,
		nextActions: [swapInstruction, ...stopLossInstruction ? [stopLossInstruction] : []],
		message: safety.warnings.length ? `Swap is ready with ${safety.warnings.length} warning(s). Execute defi_swap to proceed.` : "All checks passed. Execute defi_swap to proceed."
	});
}
async function handleLaunchAndPromote(params) {
	const name = readStringParam(params, "name", { required: true });
	const symbol = readStringParam(params, "symbol", { required: true });
	const description = readStringParam(params, "description") ?? "";
	const devBuyEth = readStringParam(params, "dev_buy_eth");
	const steps = [];
	const state = getWalletState();
	if (!state.connected) return errorResult("No wallet connected. Use clawnchconnect tool first.");
	steps.push({
		step: "wallet_check",
		status: "ok",
		data: { address: state.address }
	});
	const safety = await validateLaunch({ devBuyEth: devBuyEth ? parseFloat(devBuyEth) : void 0 });
	steps.push({
		step: "balance_check",
		status: safety.safe ? "ok" : "blocked",
		data: safety
	});
	if (!safety.safe) return jsonResult({
		workflow: "launch_and_promote",
		status: "blocked",
		steps,
		message: "Launch blocked: " + safety.blockers.join("; ")
	});
	const launchInstruction = {
		tool: "clawnch_launch",
		params: {
			name,
			symbol,
			description,
			dev_buy_eth: devBuyEth
		}
	};
	steps.push({
		step: "launch_ready",
		status: "ready",
		data: launchInstruction
	});
	const tweetInstruction = {
		tool: "clawnx",
		action: "post",
		params: {
			content: `Just launched $${symbol} (${name}) on @clawnch! 🦞\n\n${description ? description + "\n\n" : ""}Trade it now on Base via Uniswap V4.\n1% LP fees, MEV protection, fully decentralized.\n\n#DeFi #Base #${symbol}`,
			note: "Post after launch succeeds. Replace with actual token address URL."
		}
	};
	steps.push({
		step: "tweet_prepared",
		status: "ready",
		data: tweetInstruction
	});
	const monitorInstruction = {
		tool: "watch_activity",
		action: "token_activity",
		params: { note: "Use the token address from the launch result to monitor trading activity." }
	};
	steps.push({
		step: "monitor_prepared",
		status: "ready",
		data: monitorInstruction
	});
	return jsonResult({
		workflow: "launch_and_promote",
		status: "ready",
		steps,
		nextActions: [
			launchInstruction,
			tweetInstruction,
			monitorInstruction
		],
		message: "All pre-flight checks passed. Execute clawnch_launch to deploy, then follow up with tweet and monitoring."
	});
}
async function handleCheckOrders() {
	return jsonResult({
		workflow: "check_orders",
		status: "ready",
		nextActions: [{
			tool: "manage_orders",
			action: "check",
			note: "The check action now auto-fetches prices from DexScreener. Pass a token param if your orders use a token address."
		}],
		message: "Call manage_orders with action \"check\" to auto-check triggers with live prices. Any triggered orders will include execution instructions for defi_swap."
	});
}
async function handlePortfolioSnapshot() {
	const state = getWalletState();
	if (!state.connected || !state.address) return errorResult("No wallet connected. Use clawnchconnect tool first.");
	const steps = [];
	let ethBalance = "0";
	let ethValueUsd = 0;
	try {
		const { formatEther } = await import("../../_esm-BrzUp3yp.mjs").then((n) => n.t);
		const { requirePublicClient } = await import("../services/walletconnect-service.mjs");
		ethBalance = formatEther(await requirePublicClient().getBalance({ address: state.address }));
		const ethPrice = await getEthPrice();
		ethValueUsd = parseFloat(ethBalance) * ethPrice;
		steps.push({
			step: "eth_balance",
			status: "ok",
			data: {
				balance: ethBalance,
				priceUsd: ethPrice,
				valueUsd: ethValueUsd
			}
		});
	} catch (err) {
		steps.push({
			step: "eth_balance",
			status: "error",
			data: String(err)
		});
	}
	const txHistory = getTransactionHistory();
	const recentTxs = txHistory.slice(-10).reverse();
	steps.push({
		step: "recent_transactions",
		status: "ok",
		data: {
			total: txHistory.length,
			recent: recentTxs.map((tx) => ({
				status: tx.status,
				summary: tx.summary,
				hash: tx.hash,
				policyLabel: tx.policyLabel
			}))
		}
	});
	steps.push({
		step: "policies",
		status: "ok",
		data: {
			count: state.policies.length,
			mode: state.mode
		}
	});
	return jsonResult({
		workflow: "portfolio_snapshot",
		status: "ok",
		address: state.address,
		chainId: state.chainId,
		ethBalance,
		ethValueUsd,
		transactionCount: txHistory.length,
		steps,
		message: "For detailed ERC-20 balances, also call defi_balance with action \"tokens\". For fee revenue, call clawnch_fees with action \"check\"."
	});
}
//#endregion
export { createCryptoWorkflowTool };

//# sourceMappingURL=crypto-workflow.mjs.map