const { app } = require('electron');
const path = require('path');
const fs = require('fs');

const configPath = path.join(app.getPath('userData'), 'config.json');

function readConfig() {
  try {
    return JSON.parse(fs.readFileSync(configPath, 'utf8'));
  } catch {
    return {};
  }
}

function writeConfig(data) {
  try {
    fs.writeFileSync(configPath, JSON.stringify(data, null, 2));
  } catch {}
}

function updateConfig(updater) {
  const cfg = readConfig();
  updater(cfg);
  writeConfig(cfg);
}

module.exports = {
  readConfig,
  updateConfig,
  writeConfig,
};
