"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.logRawLine = logRawLine;
exports.logParsedEvent = logParsedEvent;
exports.logServerMessage = logServerMessage;
exports.logClientMessage = logClientMessage;
exports.logStatus = logStatus;
exports.logDiag = logDiag;
exports.isDebugEnabled = isDebugEnabled;
exports.debugLog = debugLog;
const promises_1 = require("node:fs/promises");
const node_os_1 = require("node:os");
const node_path_1 = __importDefault(require("node:path"));
const enabled = process.env.COCKPIT_DEBUG === "1";
const MAX_SIZE_BYTES = 50 * 1024 * 1024; // 50 MB
const CHECK_INTERVAL = 500; // check size every N writes
let logPath = null;
let prevPath = null;
let ready = null;
let writeCount = 0;
let rotating = false;
function init() {
    if (ready)
        return ready;
    const dir = node_path_1.default.join((0, node_os_1.homedir)(), ".cockpit");
    logPath = node_path_1.default.join(dir, "debug.jsonl");
    prevPath = node_path_1.default.join(dir, "debug.prev.jsonl");
    ready = (0, promises_1.mkdir)(dir, { recursive: true }).then(() => { });
    return ready;
}
function maybeRotate() {
    if (!logPath || !prevPath || rotating)
        return;
    writeCount++;
    if (writeCount % CHECK_INTERVAL !== 0)
        return;
    rotating = true;
    (0, promises_1.stat)(logPath)
        .then((s) => {
        if (s.size > MAX_SIZE_BYTES) {
            return (0, promises_1.rename)(logPath, prevPath);
        }
    })
        .catch(() => { })
        .finally(() => {
        rotating = false;
    });
}
function write(entry) {
    if (!enabled || !logPath)
        return;
    const line = JSON.stringify({ ts: Date.now(), ...entry }) + "\n";
    (0, promises_1.appendFile)(logPath, line).catch(() => { });
    maybeRotate();
}
/** Log a raw CLI stdout line before parsing */
function logRawLine(sessionId, line) {
    if (!enabled)
        return;
    init().then(() => write({ dir: "raw", sessionId, raw: line }));
}
/** Log a raw ParsedEvent from the session manager */
function logParsedEvent(sessionId, event) {
    if (!enabled)
        return;
    init().then(() => write({ dir: "event", sessionId, ...event }));
}
/** Log a ServerMessage sent to a client */
function logServerMessage(msg) {
    if (!enabled)
        return;
    init().then(() => write({ dir: "out", ...msg }));
}
/** Log a ClientMessage received from a client */
function logClientMessage(msg) {
    if (!enabled)
        return;
    init().then(() => write({ dir: "in", ...msg }));
}
/** Log session status changes */
function logStatus(sessionId, status) {
    if (!enabled)
        return;
    init().then(() => write({ dir: "status", sessionId, status }));
}
/** Log a diagnostic event with a label and arbitrary data */
function logDiag(sessionId, label, data) {
    if (!enabled)
        return;
    init().then(() => write({ dir: "diag", sessionId, label, ...data }));
}
function isDebugEnabled() {
    return enabled;
}
/** console.log gated by COCKPIT_DEBUG=1. Use for diagnostic stdout. */
function debugLog(...args) {
    if (!enabled)
        return;
    console.log(...args);
}
