package com.github.nullptroma.wallenc.infrastructure.android.db.app.repository

import com.github.nullptroma.wallenc.infrastructure.android.db.app.dao.StorageKeyMapDao
import com.github.nullptroma.wallenc.infrastructure.android.db.app.model.DbStorageKeyMap
import com.github.nullptroma.wallenc.domain.vault.model.StorageKeyMap
import com.github.nullptroma.wallenc.domain.vault.ports.StorageKeyMapStore
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.withContext

class StorageKeyMapRepository(
    private val dao: StorageKeyMapDao,
    private val ioDispatcher: CoroutineDispatcher
) : StorageKeyMapStore {
    override suspend fun getAll() = withContext(ioDispatcher) { dao.getAll().map { it.toModel() } }
    override suspend fun add(value: StorageKeyMap) = withContext(ioDispatcher) {
        val dbModel = DbStorageKeyMap.fromModel(value)
        dao.add(dbModel)
    }

    suspend fun add(vararg keymaps: StorageKeyMap) = withContext(ioDispatcher) {
        val dbModels = keymaps.map { DbStorageKeyMap.fromModel(it) }
        dao.add(*dbModels.toTypedArray())
    }

    override suspend fun delete(vararg values: StorageKeyMap) = withContext(ioDispatcher) {
        val dbModels = values.map { DbStorageKeyMap.fromModel(it) }
        dao.delete(*dbModels.toTypedArray())
    }
}
