package moe.lava.neon.api.gateway.content

import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.json.JsonContentPolymorphicSerializer
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.booleanOrNull
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import moe.lava.neon.api.objects.Guild

object GuildSerializer : JsonContentPolymorphicSerializer<Guild>(Guild::class) {
    override fun selectDeserializer(element: JsonElement): DeserializationStrategy<Guild> {
        return if (element.jsonObject["unavailable"]?.jsonPrimitive?.booleanOrNull == true) {
            Guild.Unavailable.serializer()
        } else {
            Guild.Gateway.serializer()
        }
    }
}
