"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSessionPrefs = getSessionPrefs;
exports.setSessionPrefs = setSessionPrefs;
exports.deleteSessionPrefs = deleteSessionPrefs;
exports.findChainForCliSession = findChainForCliSession;
const node_fs_1 = require("node:fs");
const node_os_1 = require("node:os");
const node_path_1 = require("node:path");
const PREFS_DIR = (0, node_path_1.join)((0, node_os_1.homedir)(), ".cockpit");
const PREFS_FILE = (0, node_path_1.join)(PREFS_DIR, "session-prefs.json");
let cache = null;
function load() {
    if (cache)
        return cache;
    try {
        cache = JSON.parse((0, node_fs_1.readFileSync)(PREFS_FILE, "utf-8"));
        return cache;
    }
    catch {
        cache = {};
        return cache;
    }
}
function save() {
    if (!cache)
        return;
    try {
        (0, node_fs_1.mkdirSync)(PREFS_DIR, { recursive: true });
        (0, node_fs_1.writeFileSync)(PREFS_FILE, JSON.stringify(cache, null, 2) + "\n");
    }
    catch {
        // best effort
    }
}
function getSessionPrefs(sessionId) {
    const all = load();
    // Prefer chain resolution. The sidebar lists entries keyed by CLI id (one
    // per transcript on disk), so navigating to a /cleared chain member should
    // pick up the canonical cockpit-keyed prefs. Checking chain first also
    // guards against legacy CLI-id duplicates that may exist in the prefs file
    // from before chain resolution was applied at write time.
    const chain = findChainForCliSession(sessionId);
    if (chain && all[chain.cockpitId])
        return all[chain.cockpitId];
    return all[sessionId];
}
function setSessionPrefs(sessionId, prefs) {
    const all = load();
    let key = sessionId;
    // If sessionId isn't already a known cockpit-id key, check whether it's a
    // CLI id in an existing chain and write under the cockpit id instead. This
    // prevents per-CLI-id duplicate entries from accumulating in the prefs file
    // when users navigate via sidebar links (which use CLI ids).
    if (!all[sessionId]) {
        const chain = findChainForCliSession(sessionId);
        if (chain && all[chain.cockpitId])
            key = chain.cockpitId;
    }
    const existing = all[key] || {};
    all[key] = { ...existing, ...prefs };
    save();
}
function deleteSessionPrefs(sessionId) {
    const all = load();
    delete all[sessionId];
    save();
}
function findChainForCliSession(targetCliId) {
    const all = load();
    for (const [cockpitId, prefs] of Object.entries(all)) {
        if (!prefs.previousCliSessionIds || !prefs.cliSessionId)
            continue;
        if (prefs.cliSessionId === targetCliId) {
            return { cockpitId, truncatedPrevIds: [...prefs.previousCliSessionIds] };
        }
        const idx = prefs.previousCliSessionIds.indexOf(targetCliId);
        if (idx >= 0) {
            return { cockpitId, truncatedPrevIds: prefs.previousCliSessionIds.slice(0, idx) };
        }
    }
    return null;
}
