import crypto from "node:crypto";
export function getTelegramChatKind(chatType) {
    switch (chatType) {
        case "private":
            return "dm";
        case "group":
            return "group";
        case "supergroup":
            return "supergroup";
        default:
            return null;
    }
}
export function buildTelegramTitle(message) {
    if (message.chat.type === "private") {
        const fullName = [message.from?.first_name, message.from?.last_name].filter(Boolean).join(" ");
        if (fullName) {
            return fullName;
        }
        return message.from?.username ?? message.chat.username ?? `user ${message.chat.id}`;
    }
    return message.chat.title ?? `${message.chat.type} ${message.chat.id}`;
}
export function buildTelegramPreview(message) {
    const text = message.text?.trim() ?? message.caption?.trim();
    if (text) {
        return compactWhitespace(text).slice(0, 120);
    }
    if (message.photo?.length)
        return "[photo]";
    if (message.document)
        return `[document: ${message.document.file_name ?? "attachment"}]`;
    if (message.voice)
        return "[voice]";
    if (message.video)
        return "[video]";
    if (message.sticker)
        return "[sticker]";
    return "[unsupported message]";
}
export function createTelegramAccessEntry(message) {
    const kind = getTelegramChatKind(message.chat.type);
    if (!kind) {
        throw new Error(`Unsupported Telegram chat type: ${message.chat.type}`);
    }
    const now = new Date(message.date * 1000).toISOString();
    return {
        id: `tg_${crypto.randomBytes(3).toString("hex")}`,
        chatId: message.chat.id,
        userId: message.from?.id,
        kind,
        title: buildTelegramTitle(message),
        username: message.from?.username ?? message.chat.username,
        status: "pending",
        firstSeenAt: now,
        lastSeenAt: now,
        pendingMessageCount: 0,
        lastMessagePreview: buildTelegramPreview(message),
    };
}
export function serializeTelegramMessage(message, entryId) {
    return {
        entryId,
        message_id: message.message_id,
        date: message.date,
        text: message.text,
        caption: message.caption,
        chat: {
            id: message.chat.id,
            type: message.chat.type,
            title: message.chat.title,
            first_name: message.chat.first_name,
            last_name: message.chat.last_name,
            username: message.chat.username,
        },
        from: message.from
            ? {
                id: message.from.id,
                first_name: message.from.first_name,
                last_name: message.from.last_name,
                username: message.from.username,
            }
            : undefined,
        photo: message.photo?.map((photo) => ({ file_id: photo.file_id })),
        document: message.document
            ? {
                file_id: message.document.file_id,
                file_name: message.document.file_name,
                mime_type: message.document.mime_type,
            }
            : undefined,
        voice: message.voice
            ? {
                file_id: message.voice.file_id,
                mime_type: message.voice.mime_type,
            }
            : undefined,
        video: message.video
            ? {
                file_id: message.video.file_id,
                mime_type: message.video.mime_type,
            }
            : undefined,
        sticker: message.sticker
            ? {
                file_id: message.sticker.file_id,
            }
            : undefined,
    };
}
export function formatTelegramAccessSummary(entry) {
    const username = entry.username ? ` @${entry.username}` : "";
    const preview = entry.lastMessagePreview ? ` — ${entry.lastMessagePreview}` : "";
    const pending = entry.pendingMessageCount > 0 ? ` (${entry.pendingMessageCount} held)` : "";
    return `${entry.id}  ${entry.kind.toUpperCase()}  ${entry.title}${username}${pending}${preview}`;
}
function compactWhitespace(value) {
    return value.replaceAll(/\s+/g, " ").trim();
}
//# sourceMappingURL=telegram-access.js.map