package dev.meloda.fast.messageshistory

import com.conena.nanokt.collections.indexOfFirstOrNull
import dev.meloda.fast.common.provider.ResourceProvider
import dev.meloda.fast.datastore.AppSettings
import dev.meloda.fast.domain.util.asPresentation
import dev.meloda.fast.messageshistory.model.MessagesHistoryScreenState
import dev.meloda.fast.model.api.domain.VkMessage
import dev.meloda.fast.ui.model.vk.MessageUiItem

fun buildMessagesHistoryUiMessages(
    messages: List<VkMessage>,
    selectedMessages: List<VkMessage>,
    screenState: MessagesHistoryScreenState,
    resourceProvider: ResourceProvider,
): List<MessageUiItem> = messages.mapIndexed { index, message ->
    message.asPresentation(
        resourceProvider = resourceProvider,
        showName = true,
        prevMessage = messages.getOrNull(index + 1),
        nextMessage = messages.getOrNull(index - 1),
        showTimeInActionMessages = AppSettings.Experimental.showTimeInActionMessages,
        convo = screenState.convo,
        isSelected = screenState.editCmId == message.cmId ||
            selectedMessages.indexOfFirstOrNull { it.id == message.id } != null
    )
}
