package de.trainingstagebuch.app.utils

import de.trainingstagebuch.app.R

object ViewModelStateUtils {
    fun normalizeUiText(
        text: UiText?,
        fallback: UiText = UiText(R.string.error_unknown)
    ): UiText {
        val firstArg = text?.args?.firstOrNull() as? String
        if (firstArg != null && firstArg.trim().isBlank()) return fallback
        return text ?: fallback
    }

    fun dynamicOrFallback(
        rawMessage: String?,
        fallback: UiText = UiText(R.string.error_unknown)
    ): UiText {
        val normalized = rawMessage?.trim().orEmpty()
        if (normalized.isBlank()) return fallback
        return UiText(
            resId = R.string.error_dynamic,
            args = listOf(normalized)
        )
    }
}
