package com.sortug.thaiinput

import android.util.Log
import com.google.gson.annotations.SerializedName

sealed interface InputToolsResponse{
    data class Success(
        val results: List<TransliterationResult>
    ): InputToolsResponse
    data object FailedToParse: InputToolsResponse
}

data class ParsedResponse(
    val status: String,
    val results: List<TransliterationResult>
)

data class TransliterationResult(
    val wholeString: String,
    val options: List<String>,
    val metadata: Map<String, Any>?
)
public fun parseResponse(response: List<Any>): InputToolsResponse {
    Log.d("THAI_IME", "parsing API response: $response")
    return when (response[0] as String) {
        "SUCCESS" -> {
            val dataList = response[1] as List<*>
            val results = dataList.map { item ->
                val itemList = item as List<*>
                TransliterationResult(
                    wholeString = itemList[0] as String,
                    options = (itemList[1] as List<*>).filterIsInstance<String>(),
                    metadata = if (itemList.size > 3) itemList[3] as? Map<String, Any> else null
                )
            }
            InputToolsResponse.Success(results)
        }
        "FAILED_TO_PARSE_REQUEST_BODY" -> InputToolsResponse.FailedToParse
        else -> throw IllegalStateException("Unknown response status: ${response[0]}")
    }
}