package com.tangem.features.onramp.tokenlist.entity.transformer

import com.tangem.common.ui.account.AccountCryptoPortfolioItemStateConverter
import com.tangem.common.ui.account.TokensListPortfolioItemConverter
import com.tangem.core.ui.components.tokenlist.state.TokensListItemUM
import com.tangem.domain.appcurrency.model.AppCurrency
import com.tangem.domain.models.TotalFiatBalance
import com.tangem.domain.models.account.AccountStatus
import com.tangem.utils.converter.Converter
import kotlinx.collections.immutable.toPersistentList

internal class LoadingAccountTokenItemConverter(
    private val appCurrency: AppCurrency,
) : Converter<AccountStatus.CryptoPortfolio, TokensListItemUM.Portfolio> {

    override fun convert(value: AccountStatus.CryptoPortfolio): TokensListItemUM.Portfolio {
        val (account, currencies) = value

        return TokensListPortfolioItemConverter(
            tokenItemUM = AccountCryptoPortfolioItemStateConverter(
                appCurrency = appCurrency,
                account = account,
                onItemClick = null,
            ).convert(TotalFiatBalance.Failed),
            isExpanded = true,
            isCollapsable = false,
            tokens = currencies.flattenCurrencies()
                .map { LoadingTokenListItemConverter.convert(it.currency) }
                .toPersistentList(),
        ).convert(Unit)
    }
}