package eu.friedcutlet.yokan.database import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable /** Source identity excludes installed version and local row ID, which can change during restore. */ @Serializable data class DbPresetSource( val name: String, val baseUrl: String, val lang: String, val itemType: DbItemType, val runtime: DbSourceCodeLanguage, val typeSource: String, val repository: String, ) @Serializable data class DbPresetOption(val label: String, val value: String) @Serializable data class DbPresetFilter( val id: String, val name: String, val type: String, val kind: String, val stableId: Boolean, val options: List, val defaultValue: String?, val defaultAscending: Boolean = true, val selection: DbPresetSelection? = null, ) @Serializable sealed interface DbPresetSelection { @Serializable @SerialName("toggle") data class Toggle(val checked: Boolean) : DbPresetSelection @Serializable @SerialName("single") data class Single(val value: String) : DbPresetSelection @Serializable @SerialName("multi") data class Multi(val values: Set) : DbPresetSelection @Serializable @SerialName("triState") data class TriState(val state: Int) : DbPresetSelection @Serializable @SerialName("text") data class Text(val value: String) : DbPresetSelection @Serializable @SerialName("sort") data class Sort(val value: String, val ascending: Boolean) : DbPresetSelection } fun DbSource.presetIdentity(): DbPresetSource = DbPresetSource( name = name.trim(), baseUrl = baseUrl.trim().trimEnd('/'), lang = lang, itemType = itemType, runtime = sourceCodeLanguage, typeSource = typeSource, repository = repo?.jsonUrl.orEmpty().trim().trimEnd('/'), ) /** Bind legacy query-only rows while their original source still exists. */ fun DbSettings.adoptLegacySourcePresets(sources: List): DbSettings { if (savedSearches.orEmpty().none { it.sourceIdentity == null && !it.missingSourceIdentity }) return this val byId = sources.associateBy { it.id } return copy(savedSearches = savedSearches.orEmpty().map { preset -> if (preset.sourceIdentity != null || preset.missingSourceIdentity) preset else { val source = byId[preset.sourceId] preset.copy(sourceIdentity = source?.presetIdentity(), missingSourceIdentity = source == null) } }) }