import { recentLarkMessageIdsByWorkspace, LARK_MESSAGE_DEDUPE_TTL_MS, } from './types.js';
export function isDuplicateLarkMessage(workspaceId, data) {
    const id = data.message?.message_id
        ?? buildFallbackLarkMessageId(data)
        ?? data.header?.event_id
        ?? data.event_id;
    if (!id)
        return false;
    const now = Date.now();
    const seen = recentLarkMessageIdsByWorkspace.get(workspaceId) ?? new Map();
    for (const [key, timestamp] of seen) {
        if (now - timestamp > LARK_MESSAGE_DEDUPE_TTL_MS)
            seen.delete(key);
    }
    recentLarkMessageIdsByWorkspace.set(workspaceId, seen);
    if (seen.has(id))
        return true;
    seen.set(id, now);
    return false;
}
function buildFallbackLarkMessageId(data) {
    const chatId = data.message?.chat_id;
    const content = data.message?.content;
    const createTime = data.message?.create_time;
    if (!chatId || !content)
        return undefined;
    return `${chatId}:${createTime ?? ''}:${content}`;
}
export function parseLarkText(content) {
    if (!content)
        return '';
    try {
        const parsed = JSON.parse(content);
        return parsed.text?.trim() ?? '';
    }
    catch {
        return content.trim();
    }
}
//# sourceMappingURL=lark-api.js.map