package moe.lava.neon.core.repository

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import moe.lava.neon.api.objects.Snowflake
import moe.lava.neon.core.data.guild.GuildGatewayDataSource
import moe.lava.neon.core.model.Guild

class GuildRepository(
    gatewaySource: GuildGatewayDataSource,
) {
    init {
        gatewaySource.flow
            .onEach { guild ->
                cache[guild.id] = guild
                joined[guild.id] = guild
            }
            .launchIn(CoroutineScope(Dispatchers.IO))
    }

    private val cache = mutableMapOf<Snowflake, Guild>()
    private val joined = mutableMapOf<Snowflake, Guild>()

    fun get(id: Snowflake) = cache[id]
    fun getJoined() = joined.toMap()
}
