package com.tomhempel.rbarometer.questionnaire

/**
 * Resolves stored answers from the in-memory map using the same keys as persistence/sync.
 */
object AnswerLookup {

    fun stringValue(answers: Map<String, Any>, question: QuestionItem.StringSpinnerQuestion): String? =
        firstString(answers, listOf(question.question, question.id))

    fun valueSpinnerValue(answers: Map<String, Any>, question: QuestionItem.ValueSpinnerQuestion): String? =
        firstString(answers, listOf(question.question, question.id))

    private fun firstString(answers: Map<String, Any>, keys: List<String>): String? {
        for (key in keys) {
            if (key.isBlank()) continue
            val raw = answers[key] ?: continue
            val text = raw.toString().trim()
            if (text.isNotEmpty()) return text
        }
        return null
    }
}
