"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.getUserPref = getUserPref;
exports.setUserPref = setUserPref;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const config_1 = require("./config");
/**
 * Per-user preferences persisted across restarts.
 *
 * Shape on disk: `{ [username]: { [key]: value } }`
 *
 * Keeps tiny, non-secret client-side state (project order, ui prefs) in one
 * file rather than scattering one file per user × feature. Not for anything
 * security-sensitive — no auth data here.
 */
const PREFS_FILE = path.join(config_1.DATA_DIR, 'user-prefs.json');
let cache = null;
let cacheMtime = 0;
function readAll() {
    try {
        const stat = fs.statSync(PREFS_FILE);
        if (cache && stat.mtimeMs === cacheMtime)
            return cache;
        const raw = fs.readFileSync(PREFS_FILE, 'utf-8');
        cache = JSON.parse(raw);
        cacheMtime = stat.mtimeMs;
        return cache;
    }
    catch {
        cache = {};
        cacheMtime = 0;
        return cache;
    }
}
function writeAll(prefs) {
    (0, config_1.atomicWriteSync)(PREFS_FILE, JSON.stringify(prefs, null, 2));
    cache = prefs;
    try {
        cacheMtime = fs.statSync(PREFS_FILE).mtimeMs;
    }
    catch { /* ignore */ }
}
function getUserPref(username, key) {
    const all = readAll();
    return all[username]?.[key];
}
function setUserPref(username, key, value) {
    const all = readAll();
    const forUser = { ...(all[username] ?? {}) };
    forUser[key] = value;
    writeAll({ ...all, [username]: forUser });
}
//# sourceMappingURL=user-prefs.js.map