package ru.forbion.f7cloud.core.push

object TalkCallPushLabels {
    private val ROOM_IN_TITLE = Regex(
        """(?i)(?:incoming call in|group call (?:has )?started in|входящий звонок в|групповой звонок.*?в)\s+(.+)$""",
    )

    fun resolveRoomDisplayName(
        title: String,
        body: String,
        roomDisplayName: String?,
    ): String {
        roomDisplayName?.trim()?.takeIf { it.isNotBlank() }?.let { return it }

        val trimmedTitle = title.trim()
        ROOM_IN_TITLE.find(trimmedTitle)?.groupValues?.getOrNull(1)
            ?.trim()
            ?.takeIf { it.isNotBlank() }
            ?.let { return it }

        if (trimmedTitle.isNotBlank() && !trimmedTitle.equals("call", ignoreCase = true)) {
            return trimmedTitle
        }

        val trimmedBody = body.trim()
        if (trimmedBody.isNotBlank() && !trimmedBody.equals("call", ignoreCase = true)) {
            return trimmedBody
        }

        return "Звонок"
    }
}
