package dev.kaizen.app.db

import androidx.room.TypeConverter
import dev.kaizen.app.net.Attachment
import dev.kaizen.app.net.SearchSource
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

class Converters {
    private val json = Json { ignoreUnknownKeys = true }

    @TypeConverter
    fun attachmentsToJson(attachments: List<Attachment>): String =
        json.encodeToString(attachments)

    @TypeConverter
    fun jsonToAttachments(value: String): List<Attachment> =
        try { json.decodeFromString(value) } catch (_: Exception) { emptyList() }

    @TypeConverter
    fun sourcesToJson(sources: List<SearchSource>): String =
        json.encodeToString(sources)

    @TypeConverter
    fun jsonToSources(value: String): List<SearchSource> =
        try { json.decodeFromString(value) } catch (_: Exception) { emptyList() }
}
