package com.pixelized.desktop.lwa.repository.map

import com.pixelized.desktop.lwa.repository.campaign.CampaignStore
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import com.pixelized.shared.lwa.model.map.Map as MiniMap

class MapRepository(
    private val mapStore: MapStore,
    private val campaignStore: CampaignStore,
) {
    fun mapsFlow(): StateFlow<Map<String, MiniMap>> = mapStore.mapsFlow()

    fun maps() = mapStore.mapsFlow().value

    fun currentMap(): Flow<MiniMap?> = combine(
        campaignStore.campaignFlow(),
        mapStore.mapsFlow(),
    ) { campaign, maps ->
        maps[campaign.map?.id]
    }

    suspend fun updateMapFlow() {
        mapStore.updateMapFlow()
    }

    @Throws
    suspend fun updateMap(
        map: MiniMap,
        create: Boolean,
    ) {
        mapStore.putMap(
            map = map,
            create = create,
        )
    }

    @Throws
    suspend fun deleteMap(
        mapId: String,
    ) {
        mapStore.deleteMap(mapId = mapId)
    }
}
