import { db } from '../../../lib/db.mjs';

export function readApplicationSettings() {
  const rows = db.prepare('SELECT key, value FROM settings').all();
  return Object.fromEntries(rows.map(row => [row.key, row.value]));
}

export function writeApplicationSettings(settings) {
  const stmt = db.prepare('INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)');
  const transaction = db.transaction(entries => {
    for (const [key, value] of entries) stmt.run(key, value == null ? '' : String(value));
  });
  transaction(Object.entries(settings));
  return readApplicationSettings();
}
