package com.tangem.feature.wallet.presentation.wallet.state.transformers

import com.tangem.domain.models.wallet.UserWallet
import com.tangem.domain.models.wallet.UserWalletId
import com.tangem.domain.wallets.usecase.GetWalletIconUseCase
import com.tangem.feature.wallet.child.wallet.model.intents.WalletClickIntents
import com.tangem.feature.wallet.presentation.wallet.domain.WalletImageResolver
import com.tangem.feature.wallet.presentation.wallet.state.model.WalletScreenState
import com.tangem.feature.wallet.presentation.wallet.state.utils.WalletLoadingStateFactory
import kotlinx.collections.immutable.toImmutableList

/**
 * Reinitialize and place new wallet at the end transformer
 *
 * @property prevWalletId  reinitialized wallet id
 * @property newUserWallet new user wallet
 * @property clickIntents  click intents
 *
[REDACTED_AUTHOR]
 */
@Suppress("LongParameterList")
internal class ReinitializeNewWalletTransformer(
    private val prevWalletId: UserWalletId,
    private val newUserWallet: UserWallet,
    private val clickIntents: WalletClickIntents,
    private val walletImageResolver: WalletImageResolver,
    private val getWalletIconUseCase: GetWalletIconUseCase,
    private val isAddFundsStage1Enabled: Boolean,
    private val isManageFundsEnabled: Boolean,
) : WalletScreenStateTransformer {

    private val walletLoadingStateFactory by lazy {
        WalletLoadingStateFactory(
            clickIntents = clickIntents,
            walletImageResolver = walletImageResolver,
            getWalletIconUseCase = getWalletIconUseCase,
            isAddFundsStage1Enabled = isAddFundsStage1Enabled,
            isManageFundsEnabled = isManageFundsEnabled,
        )
    }

    override fun transform(prevState: WalletScreenState): WalletScreenState {
        return prevState.copy(
            wallets = prevState.wallets
                .filterNot { it.walletCardState.id == prevWalletId }
                .plus(
                    element = walletLoadingStateFactory.create(
                        userWallet = newUserWallet,
                    ),
                )
                .toImmutableList(),
            wallets2 = prevState.wallets2
                .filterNot { it.walletsBalanceUM.id == prevWalletId }
                .plus(
                    element = walletLoadingStateFactory.create2(
                        userWallet = newUserWallet,
                    ),
                ).toImmutableList(),
        )
    }
}