"use strict";
/**
 * Copyright (c) 2026 ByteDance Ltd. and/or its affiliates
 * SPDX-License-Identifier: MIT
 *
 * @mention utilities for the Lark/Feishu channel plugin.
 *
 * All logic is based on `MentionInfo[]` from `MessageContext.mentions`.
 * Provides:
 * - Derive helpers: `mentionedBot()`, `nonBotMentions()`
 * - Format helpers for outbound text and card messages.
 */
import { escapeRegExp } from '../converters/utils';
// ---------------------------------------------------------------------------
// Derive helpers (work on MentionInfo[])
// ---------------------------------------------------------------------------
/** Whether the bot was @-mentioned. */
export function mentionedBot(ctx) {
    return ctx.mentions.some((m) => m.isBot);
}
/** All non-bot mentions. */
export function nonBotMentions(ctx) {
    return ctx.mentions.filter((m) => !m.isBot);
}
// ---------------------------------------------------------------------------
// extractMessageBody
// ---------------------------------------------------------------------------
/**
 * Remove all @mention placeholder keys from the message text.
 */
export function extractMessageBody(text, allMentionKeys) {
    let result = text;
    for (const key of allMentionKeys) {
        result = result.replace(new RegExp(escapeRegExp(key) + '\\s*', 'g'), '');
    }
    return result.trim();
}
// ---------------------------------------------------------------------------
// Format helpers -- text messages
// ---------------------------------------------------------------------------
/**
 * Format a mention for a Feishu text / post message.
 * @returns e.g. `<at user_id="ou_xxx">Alice</at>`
 */
export function formatMentionForText(target) {
    return `<at user_id="${target.openId}">${target.name}</at>`;
}
/** Format an @everyone mention for text / post. */
export function formatMentionAllForText() {
    return `<at user_id="all">Everyone</at>`;
}
// ---------------------------------------------------------------------------
// Format helpers -- interactive card messages
// ---------------------------------------------------------------------------
/**
 * Format a mention for a Feishu Interactive Card.
 * @returns e.g. `<at id=ou_xxx></at>`
 */
export function formatMentionForCard(target) {
    return `<at id=${target.openId}></at>`;
}
/** Format an @everyone mention for card. */
export function formatMentionAllForCard() {
    return `<at id=all></at>`;
}
// ---------------------------------------------------------------------------
// Build helpers (prepend mentions to message body)
// ---------------------------------------------------------------------------
/** Prepend @mention tags (text format) to a message body. */
export function buildMentionedMessage(targets, message) {
    if (targets.length === 0)
        return message;
    const mentionTags = targets.map(formatMentionForText).join(' ');
    return `${mentionTags}\n${message}`;
}
/** Prepend @mention tags (card format) to card markdown content. */
export function buildMentionedCardContent(targets, message) {
    if (targets.length === 0)
        return message;
    const mentionTags = targets.map(formatMentionForCard).join(' ');
    return `${mentionTags}\n${message}`;
}
