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

function getConfigPath() {
  return path.join(app.getPath('userData'), 'config.json');
}

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

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

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

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