package dev.adriankuta.flights.model.repository.mappers

import dev.adriankuta.flights.domain.types.Airport
import dev.adriankuta.flights.domain.types.MacCity
import dev.adriankuta.model.data.api.entities.RouteResponse

fun RouteResponse.Airport.MacCity.toDomain(): MacCity? {
    val macCode = macCode ?: return null
    return MacCity(macCode = macCode)
}

fun RouteResponse.toDomain(): List<Airport> {
    val departureAirportDomain = departureAirport?.let {
        Airport.Departure(
            code = it.code ?: return@let null,
            name = it.name ?: return@let null,
            macCity = departureAirport?.macCity?.toDomain(),
        )
    }
    val arrivalAirportDomain = arrivalAirport?.let {
        Airport.Arrival(
            code = it.code ?: return@let null,
            name = it.name ?: return@let null,
            macCity = arrivalAirport?.macCity?.toDomain(),
        )
    }
    val connectingAirportDomain = connectingAirport?.let {
        Airport.Connecting(
            code = it.code ?: return@let null,
            name = it.name ?: return@let null,
            macCity = connectingAirport?.macCity?.toDomain(),
        )
    }
    return listOfNotNull(departureAirportDomain, arrivalAirportDomain, connectingAirportDomain)
}
