import { Bot } from "grammy";
import { escapeHTML } from "../../core/format.js";
import { respondToExternalApproval } from "../../agents/shared/agent-approval.js";
import { contextKeyFromCtx } from "../shared/context-key.js";
import { safeEditMessage } from "./telegram-output.js";
export function registerTelegramExternalApprovalCallbacks(options) {
    options.bot.callbackQuery(/^external_approval_(yes|persist|no):([a-f0-9]+)$/, async (ctx) => {
        const action = ctx.match?.[1];
        const approvalId = ctx.match?.[2];
        const contextKey = contextKeyFromCtx(ctx);
        if (!action || !approvalId) {
            await ctx.answerCallbackQuery();
            return;
        }
        if (!contextKey) {
            await ctx.answerCallbackQuery({ text: "No chat context." });
            return;
        }
        const session = options.registry.get(contextKey);
        if (!session) {
            await ctx.answerCallbackQuery({ text: "No session for this chat." });
            return;
        }
        const result = await respondToExternalApproval(session, options.config, approvalId, action);
        await ctx.answerCallbackQuery({ text: result.message.slice(0, 200), show_alert: !result.ok });
        const chatId = ctx.chat?.id;
        const messageId = ctx.callbackQuery.message?.message_id;
        if (chatId && messageId) {
            const status = result.ok ? "Submitted" : "Failed";
            await safeEditMessage(options.bot, chatId, messageId, `${escapeHTML(ctx.callbackQuery.message?.text ?? "Action required")}\n\n<b>${escapeHTML(status)}:</b> ${escapeHTML(result.message)}`, {
                fallbackText: `${status}: ${result.message}`,
            });
        }
        options.appendActivity(ctx, contextKey, session, {
            status: result.ok ? "info" : "failed",
            type: "cli_action_required_response",
            detail: result.message,
        });
    });
}
