import { isTTSAvailable } from "../../voice/tts/factory.js";
import { appendTtsReadinessNote, formatTtsSetupHint, mergeTtsConfigFromAppConfig } from "../../voice/tts/merge-config.js";
import { commandRegistry } from "../registry.js";
import { ttsStatusTracker } from "../../voice/tts/status-tracker.js";
import "../../voice/tts/index.js";
//#region src/chat-commands/builtins/tts.ts
function defaultTtsVoiceForProvider(provider) {
	switch (provider) {
		case "openai": return "alloy";
		case "alibaba": return "Cherry";
		case "edge": return "en-US-MichelleNeural";
		case "minimax": return "male-qn-qingse";
		default: return "alloy";
	}
}
const ttsCommand = {
	id: "tts.manage",
	name: "tts",
	description: "Manage TTS (Text-to-Speech) settings",
	category: "system",
	scope: [
		"global",
		"private",
		"group"
	],
	acceptsArgs: true,
	examples: [
		"/tts",
		"/tts on",
		"/tts off",
		"/tts always",
		"/tts inbound",
		"/tts tagged",
		"/tts provider openai",
		"/tts provider minimax",
		"/tts voice alloy",
		"/tts status"
	],
	handler: async (ctx, args) => {
		const ttsConfig = (ctx.getConfig?.())?.messages?.tts;
		const isEnabled = ttsConfig?.enabled ?? false;
		const currentTrigger = ttsConfig?.trigger ?? "off";
		const currentProvider = ttsConfig?.provider ?? "openai";
		const providerSlice = ttsConfig?.providers?.[currentProvider];
		const currentVoice = typeof providerSlice?.voice === "string" && providerSlice.voice.trim() ? providerSlice.voice : defaultTtsVoiceForProvider(currentProvider);
		const ttsRuntimeOk = isTTSAvailable(mergeTtsConfigFromAppConfig(ttsConfig));
		const arg = args.trim().toLowerCase();
		const formatTimeAgo = (timestamp) => {
			const seconds = Math.floor((Date.now() - timestamp) / 1e3);
			if (seconds < 60) return `${seconds}s ago`;
			if (seconds < 3600) return `${Math.floor(seconds / 60)}m ago`;
			if (seconds < 86400) return `${Math.floor(seconds / 3600)}h ago`;
			return `${Math.floor(seconds / 86400)}d ago`;
		};
		const formatBytes = (bytes) => {
			if (bytes < 1024) return `${bytes}B`;
			if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)}KB`;
			return `${(bytes / (1024 * 1024)).toFixed(1)}MB`;
		};
		if (!arg) {
			const triggerLabels = {
				off: "Off",
				always: "Always",
				inbound: "Inbound",
				tagged: "Tagged"
			};
			const status = isEnabled ? "✅ Enabled" : "❌ Disabled";
			const trigger = triggerLabels[currentTrigger] ?? currentTrigger;
			const runtimeLine = !isEnabled ? "" : ttsRuntimeOk ? `Runtime: ✅ *Ready* (audio can be generated)
` : `Runtime: ⚠️ *Not ready* — no provider can run with current config
`;
			const setupHint = isEnabled && !ttsRuntimeOk ? `\n${formatTtsSetupHint()}\n` : "";
			return {
				content: `🔊 *TTS Settings*

Status: ${status}
Trigger Mode: *${trigger}*
Provider: *${currentProvider}*
Voice: *${currentVoice}*
` + (runtimeLine ? `\n${runtimeLine}` : "") + setupHint + "*Commands:*\n/tts on - Enable TTS\n/tts off - Disable TTS\n/tts always - Always use TTS\n/tts inbound - Only reply to voice with voice\n/tts tagged - Only use TTS with [[tts]] directive\n/tts status - Runtime TTS diagnostics (last call + stats)\n/tts provider <openai|alibaba|minimax|edge> - Set provider\n/tts voice <voice-id> - Set voice",
				success: true
			};
		}
		switch (arg) {
			case "on":
			case "enable": {
				const success = await ctx.updateConfig?.("tts.enabled", true);
				const base = success ? "✅ TTS enabled. Use `/tts always` or `/tts inbound` to set trigger mode." : "❌ Failed to enable TTS.";
				return {
					content: success ? appendTtsReadinessNote(base, ctx.getConfig?.()) : base,
					success: !!success
				};
			}
			case "off":
			case "disable": {
				const success = await ctx.updateConfig?.("tts.enabled", false);
				return {
					content: success ? "✅ TTS disabled." : "❌ Failed to disable TTS.",
					success: !!success
				};
			}
			case "always":
			case "inbound":
			case "tagged": {
				const mode = arg;
				const success = await ctx.updateConfig?.("tts.trigger", mode);
				if (success && !isEnabled) await ctx.updateConfig?.("tts.enabled", true);
				const base = success ? `✅ TTS trigger mode set to *${mode}*${!isEnabled ? " and TTS enabled" : ""}.` : `❌ Failed to set TTS trigger mode.`;
				return {
					content: success ? appendTtsReadinessNote(base, ctx.getConfig?.()) : base,
					success: !!success
				};
			}
			case "never": {
				const success = await ctx.updateConfig?.("tts.trigger", "off");
				return {
					content: success ? "✅ TTS trigger mode set to *off*." : "❌ Failed to set TTS trigger mode.",
					success: !!success
				};
			}
			case "status": {
				const status = ttsStatusTracker.getStatus();
				const lines = ["📊 *TTS Status*", ""];
				if (status.lastAttempt) {
					const last = status.lastAttempt;
					const timeAgo = formatTimeAgo(last.timestamp);
					const statusIcon = last.success ? "✅" : "❌";
					lines.push(`*Last attempt*: ${statusIcon} ${timeAgo}`);
					if (last.success) {
						lines.push(`  Provider: ${last.provider ?? "—"}`);
						lines.push(`  Latency: ${last.latencyMs ?? "—"}ms`);
						lines.push(`  Text: ${last.textLength ?? "—"} chars → Audio: ${formatBytes(last.audioSize || 0)}`);
						if (last.usedFallback) lines.push(`  ⚠️ Used fallback provider`);
						if (last.wasSummarized) lines.push(`  📝 Text was summarized`);
					} else {
						lines.push(`  Error: ${last.error ?? "—"}`);
						if (last.provider) lines.push(`  Provider: ${last.provider}`);
						lines.push(`  Latency: ${last.latencyMs ?? "—"}ms`);
					}
				} else lines.push("No TTS calls recorded yet.");
				lines.push("");
				lines.push(`*Statistics*: ${status.totalCalls} calls, ${status.totalSuccesses} success, ${status.totalFailures} failed`);
				if (status.recentSuccessRate !== void 0 && status.totalCalls > 0) {
					const rate = (status.recentSuccessRate * 100).toFixed(0);
					const window = Math.min(status.totalCalls, 20);
					lines.push(`*Recent success rate*: ${rate}% (last ${window} calls)`);
				}
				return {
					content: lines.join("\n"),
					success: true
				};
			}
			default: {
				const parts = arg.split(/\s+/);
				const subcommand = parts[0];
				const subarg = parts[1];
				if (subcommand === "provider" && subarg) {
					const provider = subarg;
					if (![
						"openai",
						"alibaba",
						"minimax",
						"edge"
					].includes(provider)) return {
						content: `❌ Invalid provider: ${provider}\nValid providers: openai, alibaba, minimax, edge`,
						success: false
					};
					const success = await ctx.updateConfig?.("tts.provider", provider);
					const base = success ? `✅ TTS provider set to *${provider}*.` : "❌ Failed to set TTS provider.";
					return {
						content: success ? appendTtsReadinessNote(base, ctx.getConfig?.()) : base,
						success: !!success
					};
				}
				if (subcommand === "voice" && subarg) {
					const voice = subarg;
					const provider = currentProvider;
					const success = await ctx.updateConfig?.(`tts.${provider}.voice`, voice);
					return {
						content: success ? `✅ TTS voice set to *${voice}* for ${provider}.` : "❌ Failed to set TTS voice.",
						success: !!success
					};
				}
				return {
					content: `❌ Unknown TTS command: ${arg}\n\nUse /tts to see available commands.`,
					success: false
				};
			}
		}
	}
};
function registerTTSCommands() {
	commandRegistry.register(ttsCommand);
}
//#endregion
export { registerTTSCommands };

//# sourceMappingURL=tts.js.map