import { renderWelcomeFirstTime, renderWelcomeReturning, renderHelpMessage, } from "./bot-ui.js";
import { authHelpText, capabilitiesOf, labelOf, } from "../shared/bot-rendering.js";
import { escapeHTML } from "../../core/format.js";
import { spawnConnectorRestart } from "../../support/operations.js";
import { renderLaunchSummaryHTML, renderLaunchSummaryPlain, renderSessionInfoHTML, renderSessionInfoPlain, } from "../shared/session-format.js";
import { safeReply } from "./telegram-output.js";
export function registerTelegramGeneralCommands(options) {
    options.bot.command("start", async (ctx) => {
        const contextSession = await options.getContextSession(ctx, { deferThreadStart: true });
        if (!contextSession) {
            return;
        }
        const { contextKey, session } = contextSession;
        const info = session.getInfo();
        const authStatus = capabilitiesOf(info).auth ? await options.checkAgentAuthStatus(info) : null;
        const authWarning = authStatus && !authStatus.authenticated
            ? [`${labelOf(info)} is not authenticated.`, authStatus.detail, authHelpText(info)].filter(Boolean).join(" ")
            : undefined;
        const isReturning = options.registry.hasMetadata(contextKey);
        if (isReturning) {
            const welcome = renderWelcomeReturning(renderSessionInfoHTML(info), renderSessionInfoPlain(info), options.isTopicContext(contextKey), authWarning);
            await safeReply(ctx, welcome.html, { fallbackText: welcome.plain });
            return;
        }
        const welcome = renderWelcomeFirstTime(authWarning);
        await safeReply(ctx, [welcome.html, "", renderLaunchSummaryHTML(info)].join("\n"), {
            fallbackText: [welcome.plain, "", renderLaunchSummaryPlain(info)].join("\n"),
        });
    });
    options.bot.command("help", async (ctx) => {
        const help = renderHelpMessage();
        await safeReply(ctx, help.html, { fallbackText: help.plain });
    });
    options.bot.command("channels", async (ctx) => {
        await options.replyChannelAction(ctx, options.commandService.renderChannels());
    });
    options.bot.command("agents", async (ctx) => {
        await options.replyChannelAction(ctx, options.commandService.renderAgents());
    });
    options.bot.command("peers", async (ctx) => {
        await options.replyChannelAction(ctx, options.commandService.renderPeers((peerId) => options.canUsePeer?.(ctx, peerId) ?? true));
    });
    options.bot.command("nodes", async (ctx) => {
        const contextSession = await options.getContextSession(ctx, { deferThreadStart: true });
        if (!contextSession)
            return;
        await options.replyChannelAction(ctx, options.commandService.renderNodeTargets({
            source: "telegram",
            contextKey: contextSession.contextKey,
            argument: "",
            preferencesStore: options.preferencesStore,
            canUsePeer: (peerId) => options.canUsePeer?.(ctx, peerId) ?? true,
        }));
    });
    options.bot.command("target", async (ctx) => {
        const contextSession = await options.getContextSession(ctx, { deferThreadStart: true });
        if (!contextSession)
            return;
        await options.replyChannelAction(ctx, options.commandService.renderTargetPreference({
            source: "telegram",
            contextKey: contextSession.contextKey,
            argument: ctx.match?.toString() ?? "",
            preferencesStore: options.preferencesStore,
            canUsePeer: (peerId) => options.canUsePeer?.(ctx, peerId) ?? true,
        }));
        options.onTargetChanged?.(contextSession.contextKey);
    });
    options.bot.command("restart", async (ctx) => {
        await safeReply(ctx, escapeHTML("Restarting connector..."), {
            fallbackText: "Restarting connector...",
        });
        setTimeout(() => {
            spawnConnectorRestart();
        }, 300);
    });
}
