import { getScheduler } from "../services/plan-scheduler.mjs";
//#region extensions/crypto/src/commands/plans-command.ts
/**
* /plans command — list and manage scheduled plans via tappable Telegram commands.
*
* Sub-commands:
*   /plans         — show all plans with status
*   /plans_active   — show only active (scheduled/running) plans
*   /plans_cancel   — show cancellable plans with IDs
*   /plans_clear    — cancel all active plans
*/
function statusIcon(status) {
	switch (status) {
		case "scheduled": return "[SCHED]";
		case "running": return "[RUN]";
		case "paused": return "[PAUSE]";
		case "completed": return "[DONE]";
		case "failed": return "[FAIL]";
		case "cancelled": return "[X]";
		case "validated": return "[READY]";
		case "draft": return "[DRAFT]";
		default: return `[${status.toUpperCase()}]`;
	}
}
function describeTrigger(plan) {
	const t = plan.trigger;
	if (!t) return "immediate";
	switch (t.type) {
		case "immediate": return "immediate";
		case "time": return `at ${new Date(t.at).toLocaleString()}`;
		case "interval": return `every ${formatMs(t.everyMs)}`;
		case "condition": return "when condition met";
		case "cron": return `cron: ${t.expression}${t.timezone ? ` (${t.timezone})` : ""}`;
		case "price": return `${t.token} ${t.condition} $${t.threshold}`;
		case "onchain_event": return `on-chain: ${t.eventSignature} on ${t.contractAddress.slice(0, 10)}...`;
		case "balance": return `${t.token} balance ${t.condition} ${t.threshold}`;
	}
}
function formatMs(ms) {
	if (ms >= 864e5) return `${Math.round(ms / 864e5)}d`;
	if (ms >= 36e5) return `${Math.round(ms / 36e5)}h`;
	if (ms >= 6e4) return `${Math.round(ms / 6e4)}m`;
	return `${Math.round(ms / 1e3)}s`;
}
function formatPlan(plan, showId = false) {
	const parts = [`${statusIcon(plan.status)} **${plan.name}**`, `  Trigger: ${describeTrigger(plan)}`];
	if (showId) parts.push(`  ID: \`${plan.id}\``);
	const age = Date.now() - plan.createdAt;
	parts.push(`  Created: ${formatMs(age)} ago`);
	return parts.join("\n");
}
const plansCommand = {
	name: "plans",
	description: "List scheduled plans",
	acceptsArgs: false,
	requireAuth: true,
	handler: async () => {
		const plans = getScheduler().listPlans();
		if (plans.length === 0) return { text: "No plans found.\n\nAsk me to create one: \"when ETH hits $4000, sell half for USDC\"" };
		const active = [
			"scheduled",
			"running",
			"paused",
			"validated",
			"draft"
		];
		plans.sort((a, b) => {
			const aActive = active.includes(a.status) ? 0 : 1;
			const bActive = active.includes(b.status) ? 0 : 1;
			if (aActive !== bActive) return aActive - bActive;
			return b.createdAt - a.createdAt;
		});
		const lines = [
			`**Plans** (${plans.length} total)`,
			"",
			...plans.slice(0, 15).map((p) => formatPlan(p, true))
		];
		if (plans.length > 15) lines.push(`\n...and ${plans.length - 15} more`);
		const activeCount = plans.filter((p) => active.includes(p.status)).length;
		lines.push("");
		lines.push(`Active: ${activeCount} | /plans_active | /plans_cancel`);
		return { text: lines.join("\n") };
	}
};
const plansActiveCommand = {
	name: "plans_active",
	description: "Show active plans only",
	acceptsArgs: false,
	requireAuth: true,
	handler: async () => {
		const plans = getScheduler().listPlans().filter((p) => [
			"scheduled",
			"running",
			"paused"
		].includes(p.status));
		if (plans.length === 0) return { text: "No active plans.\n\nAsk me to create one, or tap /plans to see all plans." };
		return { text: [
			`**Active Plans** (${plans.length})`,
			"",
			...plans.map((p) => formatPlan(p, true))
		].join("\n") };
	}
};
const plansCancelCommand = {
	name: "plans_cancel",
	description: "Show plans that can be cancelled (with IDs)",
	acceptsArgs: true,
	requireAuth: true,
	handler: async (ctx) => {
		const scheduler = getScheduler();
		const planId = (ctx?.args ?? ctx?.commandBody ?? "").trim();
		if (planId) {
			if (!scheduler.cancelPlan(planId)) return { text: `Plan \`${planId}\` not found or already completed.` };
			return { text: `Cancelled plan \`${planId}\`.` };
		}
		const cancellable = scheduler.listPlans().filter((p) => [
			"scheduled",
			"running",
			"paused",
			"validated",
			"draft"
		].includes(p.status));
		if (cancellable.length === 0) return { text: "No plans to cancel." };
		return { text: [
			`**Cancellable Plans** (${cancellable.length})`,
			"",
			...cancellable.map((p) => formatPlan(p, true)),
			"",
			"To cancel a plan, tell me: \"cancel plan PLAN_ID\"",
			"To cancel all: /plans_clear"
		].join("\n") };
	}
};
const plansClearCommand = {
	name: "plans_clear",
	description: "Cancel all active plans",
	acceptsArgs: false,
	requireAuth: true,
	handler: async () => {
		const scheduler = getScheduler();
		const active = scheduler.listPlans().filter((p) => [
			"scheduled",
			"running",
			"paused",
			"validated",
			"draft"
		].includes(p.status));
		if (active.length === 0) return { text: "No active plans to clear." };
		let cancelled = 0;
		for (const plan of active) if (scheduler.cancelPlan(plan.id)) cancelled++;
		return { text: `Cancelled ${cancelled} plan${cancelled !== 1 ? "s" : ""}.\n\nAll scheduled operations have been stopped.` };
	}
};
//#endregion
export { plansActiveCommand, plansCancelCommand, plansClearCommand, plansCommand };

//# sourceMappingURL=plans-command.mjs.map