'use strict'; // App settings (not notes): last vault, theme, window size, folded pages. const fs = require('node:fs'); const path = require('node:path'); let file = null; let cache = null; function location() { if (!file) file = path.join(require('electron').app.getPath('userData'), 'settings.json'); return file; } function get() { if (cache) return cache; try { cache = JSON.parse(fs.readFileSync(location(), 'utf8')); } catch { cache = {}; } return cache; } let timer = null; function set(patch) { cache = { ...get(), ...patch }; clearTimeout(timer); timer = setTimeout(flush, 200); return cache; } function flush() { clearTimeout(timer); if (!cache) return; try { fs.mkdirSync(path.dirname(location()), { recursive: true }); fs.writeFileSync(location(), JSON.stringify(cache, null, 2)); } catch (err) { console.error('Could not write settings', err); } } module.exports = { get, set, flush };