import { ref } from 'vue'
import { settingsRepository } from '../db/repositories/settingsRepository.js'

export function useSettings() {
  const settings = ref({})
  const loading = ref(false)

  async function fetchAll() {
    loading.value = true
    try {
      settings.value = await settingsRepository.getAll()
    } finally {
      loading.value = false
    }
  }

  async function get(key) {
    return settingsRepository.get(key)
  }

  async function set(key, value) {
    await settingsRepository.set(key, value)
    settings.value = { ...settings.value, [key]: value }
  }

  return { settings, loading, fetchAll, get, set }
}
