package eu.friedcutlet.yokan.ui.screens.browse import eu.friedcutlet.yokan.database.DbSavedSearch import eu.friedcutlet.yokan.database.DbSettings import eu.friedcutlet.yokan.database.DbSource import eu.friedcutlet.yokan.database.presetIdentity import eu.friedcutlet.yokan.services.extensions.PresetIssue import eu.friedcutlet.yokan.services.extensions.PresetIssueReason import eu.friedcutlet.yokan.services.extensions.PresetValidation import eu.friedcutlet.yokan.services.extensions.capturePresetFilters import eu.friedcutlet.yokan.services.extensions.validateSourcePreset import kotlin.random.Random internal fun DbSettings.withSourcePreset(preset: DbSavedSearch): DbSettings = copy( savedSearches = savedSearches.orEmpty().filterNot { it.sourceId == preset.sourceId && it.id == preset.id } + preset, ) internal fun DbSettings.withoutSourcePreset(sourceId: Long, id: String): DbSettings = copy( savedSearches = savedSearches.orEmpty().filterNot { it.sourceId == sourceId && it.id == id }, ) internal fun DbSettings.sourcePreset(sourceId: Long, id: String): DbSavedSearch? = savedSearches.orEmpty().firstOrNull { it.sourceId == sourceId && it.id == id } internal fun newPresetId(): String = "preset:" + Random.nextBytes(16).toHexString() /** Result of committing the open [PresetEditor]: the editor to show next and the preset to store, if any. */ internal data class PresetCommit(val editor: PresetEditor?, val save: DbSavedSearch? = null) /** * Commits [state]'s preset editor for [source]. Returns null when nothing changes (no editor, or the * filter schema is not ready for a save/update); otherwise the next editor (kept open with an error or * conflict, or closed) and the preset to store. */ internal fun commitPreset( state: SourceHomeUiState, db: DbSettings, source: DbSource, replace: Boolean, newId: () -> String = ::newPresetId, ): PresetCommit? { val editor = state.presetEditor ?: return null val name = editor.name.trim() if (name.isEmpty()) return PresetCommit(editor.copy(invalidName = true)) val target = editor.targetId?.let { db.sourcePreset(source.id, it) } if (editor.targetId != null && target == null) return PresetCommit(null) val conflict = db.savedSearches.orEmpty().firstOrNull { it.sourceId == source.id && it.name == name && it.id != editor.targetId } if (conflict != null && (!replace || editor.conflictId != conflict.id || editor.mode == PresetEditMode.Rename)) { return PresetCommit(editor.copy(conflictId = conflict.id)) } if (editor.mode != PresetEditMode.Rename && !state.filterSchemaReady) return null val preset = if (editor.mode == PresetEditMode.Rename && target != null) target.copy(name = name) else DbSavedSearch( sourceId = source.id, name = name, query = state.searchQuery.trim(), id = target?.id ?: conflict?.id ?: newId(), sourceIdentity = source.presetIdentity(), filters = capturePresetFilters(state.filters, state.selectedFilterValues), filterSchemaRecorded = true, ) return PresetCommit(null, preset) } /** Runs [preset] as a search when it validates against the loaded filters; otherwise opens its review. */ internal fun SourceHomeUiState.withPresetApplied(preset: DbSavedSearch, source: DbSource): SourceHomeUiState { val validation = if (filterSchemaReady) validateSourcePreset(preset, source, filters) else PresetValidation(emptyMap(), listOf(PresetIssue(preset.name, PresetIssueReason.SchemaNotReady))) if (validation.issues.isNotEmpty()) return copy(presetReview = PresetReview(preset, validation)) return copy(isSearchMode = true, selectedTab = SourceTab.Search, searchQuery = preset.query, submittedQuery = preset.query, selectedFilterValues = validation.selections, appliedFilterValues = validation.selections, filtersApplied = validation.selections.isNotEmpty(), page = 1, items = emptyList(), hasNextPage = false, appendExhausted = false, loading = false, loadingMore = false, loadedKey = null, requestRevision = requestRevision + 1, presetReview = null) }