package com.laseroptek.raman.utils.ext

import kotlinx.coroutines.DelicateCoroutinesApi // <-- Import this
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeout

@OptIn(DelicateCoroutinesApi::class) // <-- Add this annotation
fun <T> Flow<T>.toStateFlowBlocking(timeoutMillis: Long = 5000) = runBlocking {
    withTimeout(timeoutMillis) {
        stateIn(
            GlobalScope,
            started = SharingStarted.WhileSubscribed(5000),
            initialValue = null // This will produce a StateFlow<T?>
        )
    }
}
