package com.pixelized.desktop.lwa.repository.roll_history

import com.pixelized.desktop.lwa.repository.network.NetworkRepository
import com.pixelized.shared.lwa.protocol.websocket.RollEvent
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.filterIsInstance
import kotlinx.coroutines.flow.shareIn

class RollHistoryRepository(
    network: NetworkRepository,
) {
    private val scope = CoroutineScope(Dispatchers.IO + Job())

    private val rolls: SharedFlow<RollEvent> = network.data
        .filterIsInstance(RollEvent::class)
        .shareIn(
            scope = scope,
            started = SharingStarted.Eagerly,
        )

    fun rolls(): SharedFlow<RollEvent> = rolls
}