package com.anypreta.bountyboard.di

import androidx.compose.runtime.staticCompositionLocalOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewmodel.CreationExtras
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import com.anypreta.bountyboard.data.AppContainer

/** The app's DI container, provided at the Compose root. */
val LocalAppContainer = staticCompositionLocalOf<AppContainer> {
    error("AppContainer not provided")
}

/**
 * Build a ViewModelProvider.Factory that injects the [AppContainer] into a ViewModel.
 * Usage: `viewModel(factory = containerVMFactory(container) { MyVm(it) })`.
 */
inline fun <reified VM : ViewModel> containerVMFactory(
    container: AppContainer,
    crossinline create: (AppContainer) -> VM,
) = viewModelFactory {
    initializer { create(container) }
}

@Suppress("UNUSED_PARAMETER")
fun unusedExtras(extras: CreationExtras) = Unit
