package com.tangem.feature.wallet.presentation.wallet.analytics.utils

import com.tangem.core.decompose.di.ModelScoped
import com.tangem.domain.models.wallet.UserWalletId
import com.tangem.domain.wallets.usecase.SeedPhraseNotificationUseCase
import com.tangem.feature.wallet.presentation.wallet.state.model.WalletNotification
import com.tangem.feature.wallet.presentation.wallet.state.model.WalletState
import com.tangem.feature.wallet.presentation.wallet.utils.ScreenLifecycleProvider
import javax.inject.Inject

@ModelScoped
internal class WalletWarningsSingleEventSender @Inject constructor(
    private val seedPhraseNotificationUseCase: SeedPhraseNotificationUseCase,
    private val screenLifecycleProvider: ScreenLifecycleProvider,
) {

    suspend fun send(
        userWalletId: UserWalletId,
        displayedUiState: WalletState?,
        newWarnings: List<WalletNotification>,
    ) {
        if (screenLifecycleProvider.isBackgroundState.value) return
        if (newWarnings.isEmpty()) return
        if (displayedUiState == null || displayedUiState.pullToRefreshConfig.isRefreshing) return

        val events = newWarnings.filter { it !in displayedUiState.warnings }

        events.forEach { event ->
            if (event is WalletNotification.Critical.SeedPhraseNotification) {
                seedPhraseNotificationUseCase.notified(userWalletId = userWalletId)
            }
        }
    }
}