package ru.forbion.f7cloud.feature.talk

data class TalkMention(
    val mentionId: String,
    val id: String,
    val label: String,
    val source: String = "users",
)

fun formatMentionForSend(mention: TalkMention): String {
    val id = mention.mentionId.ifBlank { mention.id.ifBlank { mention.label } }
    val quoted = id.contains(' ') ||
        id.contains('@') ||
        id.startsWith("guest/") ||
        id.startsWith("group/") ||
        id.startsWith("email/") ||
        id.startsWith("team/")
    return if (quoted) "@\"$id\"" else "@$id"
}

fun insertMentionIntoDraft(draft: String, mention: TalkMention): String {
    val atIndex = draft.lastIndexOf('@')
    if (atIndex < 0) return draft + formatMentionForSend(mention) + " "
    val before = draft.substring(0, atIndex)
    val token = formatMentionForSend(mention)
    return before + token + " "
}
