package dev.adriankuta.flights.model.data.simple

import dev.adriankuta.flights.model.data.shared.CacheImpl
import dev.adriankuta.flights.model.data.simple.mappers.toModel
import dev.adriankuta.flights.model.datasource.airports.AirportsDatasource
import dev.adriankuta.flights.model.datasource.airports.entities.AirportInfoModel
import dev.adriankuta.flights.model.datasource.shared.Cache
import dev.adriankuta.model.data.api.entities.AirportResponse
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow

internal class AirportsDatasourceImpl : AirportsDatasource {

    private val _airports = MutableStateFlow(CacheImpl.emptyCache<List<AirportInfoModel>>())

    override val airports: StateFlow<Cache<List<AirportInfoModel>>> = _airports.asStateFlow()

    override suspend fun setAirportsInfo(
        airports: List<AirportResponse>,
        cacheKey: String,
    ) {
        _airports.value = CacheImpl(
            cacheKey = cacheKey,
            data = airports.map { it.toModel() },
        )
    }
}
