"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.readConfig = readConfig;
exports.writeConfig = writeConfig;
exports.writeConclusion = writeConclusion;
exports.writeReportHtml = writeReportHtml;
exports.writeJsonFile = writeJsonFile;
exports.turnReportPath = turnReportPath;
exports.writeTurnReportHtml = writeTurnReportHtml;
exports.writeTurnMarkdown = writeTurnMarkdown;
exports.loadSession = loadSession;
/**
 * Read/write the per-session folder (`<sessionsRoot>/<sessionId>/*`).
 *
 * Everything here is synchronous on purpose — these writes happen once
 * per turn and blocking is simpler + safer than trying to coordinate
 * parallel appends with the analyze() loop.
 */
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const atomicFileWriter_1 = require("../../utils/atomicFileWriter");
const paths_1 = require("./paths");
function readConfig(sp) {
    if (!fs.existsSync(sp.config))
        return null;
    try {
        return JSON.parse(fs.readFileSync(sp.config, 'utf-8'));
    }
    catch {
        return null;
    }
}
/** Full write. For incremental updates, callers should readConfig → mutate → writeConfig. */
function writeConfig(sp, cfg) {
    (0, paths_1.ensureSessionLayout)(sp);
    (0, atomicFileWriter_1.atomicWriteFileSync)(sp.config, JSON.stringify(cfg, null, 2));
}
function writeConclusion(sp, markdown) {
    (0, paths_1.ensureSessionLayout)(sp);
    fs.writeFileSync(sp.conclusion, markdown, 'utf-8');
}
function writeReportHtml(sp, html) {
    (0, paths_1.ensureSessionLayout)(sp);
    fs.writeFileSync(sp.report, html, 'utf-8');
}
function writeJsonFile(sp, filePath, value) {
    (0, paths_1.ensureSessionLayout)(sp);
    (0, atomicFileWriter_1.atomicWriteFileSync)(filePath, JSON.stringify(value ?? null, null, 2));
}
function turnReportPath(sp, turn) {
    return path.join(sp.turnsDir, `${String(turn).padStart(3, '0')}.html`);
}
function writeTurnReportHtml(sp, turn, html) {
    (0, paths_1.ensureSessionLayout)(sp);
    const filePath = turnReportPath(sp, turn);
    fs.writeFileSync(filePath, html, 'utf-8');
    return filePath;
}
/** Per-turn markdown snapshot. `turn` is 1-indexed. */
function writeTurnMarkdown(sp, turn, markdown) {
    (0, paths_1.ensureSessionLayout)(sp);
    const filename = `${String(turn).padStart(3, '0')}.md`;
    fs.writeFileSync(path.join(sp.turnsDir, filename), markdown, 'utf-8');
}
/** Convenience: resolve paths + read config in one call. */
function loadSession(paths, sessionId) {
    const sp = (0, paths_1.sessionPaths)(paths, sessionId);
    return { sp, config: readConfig(sp) };
}
//# sourceMappingURL=sessionStore.js.map