"use strict";
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2024-2026 Gracker (Chris)
// This file is part of SmartPerfetto. See LICENSE for details.
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.readIndex = readIndex;
exports.writeIndex = writeIndex;
exports.upsertSession = upsertSession;
/**
 * Atomic read/write of ~/.smartperfetto/index.json — the global session
 * catalog used by `smartperfetto list`. Hand-maintained rather than
 * reconstructed from the filesystem each time so `list` stays O(1).
 *
 * Atomicity is delegated to `atomicWriteFileSync` (tmp + rename with
 * pid/date/random suffix), which keeps concurrent CLI invocations from
 * colliding on the tmp path.
 */
const fs = __importStar(require("fs"));
const atomicFileWriter_1 = require("../../utils/atomicFileWriter");
const EMPTY_INDEX = { version: 1, sessions: {} };
function readIndex(paths) {
    // Single read attempt — ENOENT (missing file, first run) and corrupt JSON
    // both fall through to an empty index, so there's no need for a separate
    // existsSync precheck (which would also open a TOCTOU race).
    try {
        const raw = fs.readFileSync(paths.indexFile, 'utf-8');
        const parsed = JSON.parse(raw);
        if (!parsed || typeof parsed !== 'object' || !parsed.sessions) {
            return { ...EMPTY_INDEX, sessions: {} };
        }
        return { version: 1, sessions: parsed.sessions };
    }
    catch {
        return { ...EMPTY_INDEX, sessions: {} };
    }
}
function writeIndex(paths, index) {
    (0, atomicFileWriter_1.atomicWriteFileSync)(paths.indexFile, JSON.stringify(index, null, 2));
}
/** Upsert a single session entry and atomically persist. */
function upsertSession(paths, entry) {
    const idx = readIndex(paths);
    idx.sessions[entry.sessionId] = entry;
    writeIndex(paths, idx);
}
//# sourceMappingURL=indexJson.js.map