package com.tangem.feature.wallet.presentation.wallet.domain

import arrow.core.Either
import com.tangem.domain.models.currency.CryptoCurrencyStatus
import com.tangem.domain.models.wallet.UserWallet
import com.tangem.domain.models.wallet.UserWalletId
import com.tangem.domain.tokens.GetSingleCryptoCurrencyStatusUseCase
import com.tangem.domain.tokens.error.CurrencyStatusError
import com.tangem.domain.wallets.usecase.GetSelectedWalletSyncUseCase
import kotlinx.coroutines.flow.*
import timber.log.Timber

internal fun GetSelectedWalletSyncUseCase.unwrap(): UserWallet? {
    return this().fold(
        ifLeft = {
            Timber.e("Impossible to get selected wallet $it")
            null
        },
        ifRight = { it },
    )
}

internal suspend fun GetSingleCryptoCurrencyStatusUseCase.unwrap(userWalletId: UserWalletId): CryptoCurrencyStatus? {
    return invokeSingleWallet(userWalletId)
        .conflate()
        .distinctUntilChanged()
        .filter(Either<CurrencyStatusError, CryptoCurrencyStatus>::isRight)
        .firstOrNull()
        ?.fold(
            ifLeft = {
                Timber.e("Impossible to get primary currency status $it")
                null
            },
            ifRight = { it },
        )
}

internal suspend fun GetSingleCryptoCurrencyStatusUseCase.collectLatest(
    userWalletId: UserWalletId,
    onRight: suspend (CryptoCurrencyStatus) -> Unit,
) {
    invokeSingleWallet(userWalletId = userWalletId)
        .conflate()
        .distinctUntilChanged()
        .collectLatest { maybeStatus ->
            maybeStatus.onRight { onRight(it) }
        }
}