"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "LogDumpService", {
    enumerable: true,
    get: function() {
        return LogDumpService;
    }
});
const _admzip = /*#__PURE__*/ _interop_require_default(require("adm-zip"));
const _path = require("path");
const _fs = require("fs");
const _fsutils = require("../../utils/fs.utils");
const _serverconstants = require("../../server.constants");
const _timeutils = require("../../utils/time.utils");
const _nodefs = require("node:fs");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
class LogDumpService {
    logger;
    constructor(loggerFactory){
        this.logger = loggerFactory(LogDumpService.name);
    }
    async deleteOlderThanWeekAndMismatchingLogFiles() {
        this.logger.log("Cleaning log files");
        const path = (0, _path.join)((0, _fsutils.superRootPath)(), _serverconstants.AppConstants.defaultLogsFolder);
        const dirEntries = (0, _fs.readdirSync)(path, {
            withFileTypes: true
        });
        const files = dirEntries.filter((dirent)=>dirent.isFile()).map((dirent)=>dirent.name);
        const startingFormat = `${_serverconstants.AppConstants.logAppName}-`;
        const removedFilesNotInFormat = files.filter((f)=>f.endsWith(".log") && !f.startsWith(startingFormat));
        const removedFilesOutdated = files.filter((f)=>{
            const matchesFormat = f.endsWith(".log") && f.startsWith(startingFormat);
            if (!matchesFormat) return false;
            const strippedFilename = f.replace(".log", "").replace(startingFormat, "");
            if (!(0, _timeutils.isParsableDate)(strippedFilename)) {
                this.logger.warn("Failed to parse date from log file, removing it as outdated");
                return true;
            }
            const date = new Date(strippedFilename);
            const now = new Date();
            const diff = now.getTime() - date.getTime();
            const diffDays = diff / (1000 * 3600 * 24);
            return diffDays > 7;
        });
        this.logger.log(`Removing ${removedFilesNotInFormat.length} files that are not in the format of ${startingFormat}<date>.log, and ${removedFilesOutdated.length} files that are older than 7 days`);
        let removedWrongFormatFilesCount = 0;
        for (const file of removedFilesNotInFormat){
            try {
                (0, _nodefs.rmSync)((0, _path.join)(path, file));
                removedWrongFormatFilesCount++;
            } catch (err) {
                this.logger.warn(`Failed to delete log file`);
            }
        }
        let removedOutdatedFilesCount = 0;
        for (const file of removedFilesOutdated){
            try {
                (0, _nodefs.rmSync)((0, _path.join)(path, file));
                removedOutdatedFilesCount++;
            } catch (err) {
                this.logger.warn(`Failed to delete log file`);
            }
        }
        this.logger.log(`Removed ${removedWrongFormatFilesCount + removedOutdatedFilesCount} log file(s)`);
        return {
            removedWrongFormatFilesCount,
            removedOutdatedFilesCount
        };
    }
    async dumpZip() {
        this.logger.log("Dumping logs as ZIP");
        const zip = new _admzip.default();
        const path = (0, _path.join)((0, _fsutils.superRootPath)(), _serverconstants.AppConstants.defaultLogsFolder);
        zip.addLocalFolder(path, "/logs", (filename)=>filename.endsWith(".log"));
        const outputPath = (0, _path.join)((0, _fsutils.superRootPath)(), _serverconstants.AppConstants.defaultLogZipsFolder, `logs-${_serverconstants.AppConstants.serverRepoName}.zip`);
        zip.writeZip(outputPath);
        return outputPath;
    }
}

//# sourceMappingURL=logs-manager.service.js.map