"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LocalLogManager = void 0;
/***************
local-logs.manager.ts
****************/
var fs = require("fs");
var path = require("path");
var resolveio_server_app_1 = require("../resolveio-server-app");
var LocalLogManager = /** @class */ (function () {
    function LocalLogManager() {
    }
    LocalLogManager.create = function () {
        var localLogManager = new LocalLogManager();
        localLogManager.initialize();
        return localLogManager;
    };
    LocalLogManager.prototype.initialize = function () {
        var logDir = resolveLocalLogDir();
        if (resolveio_server_app_1.ResolveIOServer.shouldWriteLogsOffline()) {
            if (!fs.existsSync(logDir)) {
                fs.mkdirSync(logDir, { recursive: true });
            }
        }
        this._logFilePath = path.join(logDir, 'local-logs.jsonl');
    };
    // Internal method to append a JSON line to the file
    LocalLogManager.prototype.writeLog = function (logEntry) {
        try {
            logEntry.instance_index = process.env.NODE_APP_INSTANCE || '';
            var log = JSON.stringify(logEntry) + '\n';
            fs.appendFileSync(this._logFilePath, log, { encoding: 'utf8' });
        }
        catch (err) {
            // If there's an error writing logs, you might want to handle it or queue it
            console.error(new Date(), 'LocalLogManager', 'Failed to write log to file:', err);
        }
    };
    LocalLogManager.prototype.writeLogs = function (logEntries) {
        if (!logEntries || logEntries.length === 0) {
            return;
        }
        try {
            // Convert each item to a JSON string, add newline
            var logs = logEntries.map(function (logEntry) {
                logEntry.instance_index = process.env.NODE_APP_INSTANCE || '';
                return JSON.stringify(logEntry);
            }).join('\n') + '\n';
            fs.appendFileSync(this._logFilePath, logs, { encoding: 'utf8' });
        }
        catch (err) {
            console.error(new Date(), 'LocalLogManager', 'Failed to write logs batch:', err);
        }
    };
    /**
     * Reads all logs from disk, then empties the file.
     */
    LocalLogManager.prototype.offloadLogs = function () {
        var result = [];
        try {
            if (!fs.existsSync(this._logFilePath)) {
                return result;
            }
            var fileData = fs.readFileSync(this._logFilePath, 'utf8');
            if (fileData.trim().length === 0) {
                return result;
            }
            result = fileData.split('\n').filter(function (l) { return l.trim() !== ''; });
            // Truncate the file so we don't send logs more than once
            fs.writeFileSync(this._logFilePath, '');
        }
        catch (err) {
            console.error(new Date(), 'LocalLogManager', 'Failed to offload logs:', err);
        }
        return result;
    };
    return LocalLogManager;
}());
exports.LocalLogManager = LocalLogManager;
function resolveLocalLogDir() {
    var overrideDir = String(process.env.RESOLVEIO_LOCAL_LOG_DIR || '').trim();
    if (overrideDir) {
        return overrideDir;
    }
    return '/home/ubuntu/resolveio/logs';
}

//# sourceMappingURL=local-log.manager.js.map
