package io.novafoundation.nova.feature_account_api.presenatation.actions

import android.content.Context
import androidx.lifecycle.lifecycleScope
import io.novafoundation.nova.common.base.BaseFragment
import io.novafoundation.nova.common.base.BaseViewModel
import io.novafoundation.nova.common.mixin.impl.observeBrowserEvents

fun <T> BaseFragment<T, *>.setupExternalActions(viewModel: T) where T : BaseViewModel, T : ExternalActions {
    setupExternalActions(viewModel) { context, payload ->
        ExternalActionsSheet(
            context,
            payload,
            viewModel::copyValue,
            viewModel::viewExternalClicked
        )
    }
}

inline fun <T> BaseFragment<T, *>.setupExternalActions(
    viewModel: T,
    crossinline customSheetCreator: suspend (Context, ExternalActions.Payload) -> ExternalActionsSheet,
) where T : BaseViewModel, T : ExternalActions {
    observeBrowserEvents(viewModel)

    viewModel.showExternalActionsEvent.observeEvent {
        viewLifecycleOwner.lifecycleScope.launchWhenResumed {
            customSheetCreator(requireContext(), it).show()
        }
    }
}
