/*
 * SPDX-License-Identifier: GPL-3.0-or-later
 */
package com.runicgateway.app.ui.shard

import kotlinx.serialization.json.JsonNull
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive

/**
 * Minimal typed reads from a raw SSE frame [JsonObject] for the fields a board's
 * `*.remove` / `*.decay` delta needs when the whole frame doesn't warrant decoding
 * into a DTO (e.g. `champ.remove` carries only a `serial`). Returns null on a
 * missing or ill-typed field so a malformed frame is skipped, not fatal.
 */
object FrameFields {
    fun longField(obj: JsonObject, key: String): Long? {
        val el = obj[key]
        if (el !is JsonPrimitive || el is JsonNull) return null
        return el.content.toLongOrNull()
    }

    fun stringField(obj: JsonObject, key: String): String? {
        val el = obj[key]
        if (el !is JsonPrimitive || el is JsonNull) return null
        return el.content
    }
}
