package com.isidroid.spot.data.mapper

import com.isidroid.spot.model.PrintProfile
import com.isidroid.spot.model.RichSpot
import com.isidroid.spot.model.Spot
import com.isidroid.spot.data.source.remote.response.SpotResponse

internal fun SpotResponse.transform(): Spot = Spot(
    id = id,
    name = name,
    description = description,
    address = address,
    status = status,
    distance = distance,
    lat = lat,
    lng = lng,
    code = code.orEmpty()
)

internal fun List<Spot>?.transformToRichSpot(printProfiles: Map<String, List<PrintProfile>?>?) = this?.map { RichSpot(spot = it, printProfiles = printProfiles?.get(it.id).orEmpty()) }


internal fun List<PrintProfile>.convertToMap(): Map<String, List<PrintProfile>> {
    val result = hashMapOf<String, MutableList<PrintProfile>>()
    for (profile in this) {
        val key = profile.spotId
        if (!result.containsKey(key))
            result[key] = mutableListOf()

        result[key]?.add(profile)
    }

    return result.mapValues { it.value.toList() }
}