package com.correx.apps.server.serialization

import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.jsonPrimitive

const val UNKNOWN_PAYLOAD_TYPE = "unknown"

// The polymorphic discriminator emitted by eventJson for every EventPayload —
// payloads always encode to a JsonObject carrying a "type" key.
fun payloadDiscriminator(element: JsonElement): String =
    (element as? JsonObject)?.get("type")?.jsonPrimitive?.content ?: UNKNOWN_PAYLOAD_TYPE
