@file:OptIn(kotlin.js.ExperimentalWasmJsInterop::class) package eu.friedcutlet.yokan.app.profiles import kotlinx.browser.localStorage import kotlinx.coroutines.NonCancellable import kotlinx.coroutines.withContext import kotlinx.coroutines.await import kotlin.js.JsAny import kotlin.js.Promise import eu.friedcutlet.yokan.domain.ProfileId import eu.friedcutlet.yokan.domain.ProfileStorageNamespace internal actual suspend fun deletePlatformProfileStorage(id: ProfileId, exportedBackups: List) { require(id != ProfileId.Default) val namespace = ProfileStorageNamespace(id) withContext(NonCancellable) { removeBrowserProfile(namespace.ownedRoot("/yokan")).await() } localStorage.removeItem(namespace.browserSnapshotKey) val prefix = "yokan-backup-file:${namespace.ownedRoot("/yokan")}/" val keys = (0 until localStorage.length).mapNotNull(localStorage::key) keys.filter { it.startsWith(prefix) && it.removePrefix("yokan-backup-file:") !in exportedBackups } .forEach(localStorage::removeItem) } private fun removeBrowserProfile(root: String): Promise = js("globalThis.yokanBrowserFiles.deleteProfile(root).then(() => null)")