"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "PrinterFileCleanTask", {
    enumerable: true,
    get: function() {
        return PrinterFileCleanTask;
    }
});
class PrinterFileCleanTask {
    logger;
    printerFilesStore;
    printerCache;
    settingsStore;
    taskManagerService;
    octoprintClient;
    constructor({ printerCache, printerFilesStore, octoprintClient, taskManagerService, settingsStore, loggerFactory }){
        this.printerCache = printerCache;
        this.printerFilesStore = printerFilesStore;
        this.taskManagerService = taskManagerService;
        this.octoprintClient = octoprintClient;
        this.settingsStore = settingsStore;
        this.logger = loggerFactory(PrinterFileCleanTask.name);
    }
    get ageDaysMaxSetting() {
        return this.getSettings().autoRemoveOldFilesCriteriumDays;
    }
    async run() {
        const printers = await this.printerCache.listCachedPrinters(false);
        const fileCleanSettings = this.getSettings();
        const autoCleanAtBootEnabled = fileCleanSettings.autoRemoveOldFilesAtBoot;
        try {
            if (autoCleanAtBootEnabled) {
                this.logger.log(`Cleaning files of ${printers.length} active printers [printerFileClean:autoRemoveOldFilesAtBoot].`);
            } else {
                this.logger.log(`Reporting about old files of ${printers.length} printers.`);
            }
            const errorPrinters = [];
            for (let printer of printers){
                try {
                    await this.printerFilesStore.loadFiles(printer.id, false);
                } catch (e) {
                    errorPrinters.push({
                        e,
                        printer
                    });
                }
            }
            if (errorPrinters.length > 0) {
                this.logger.error(`Error loading some files, ${errorPrinters.length} printer(s) did not respond or returned an unexpected status code. Those will depend on previously cached files.`);
            }
            for (let printer of printers){
                const outdatedFiles = this.getPrinterOutdatedFiles(printer);
                if (!outdatedFiles?.length) continue;
                this.logger.log(`Found ${outdatedFiles?.length} old files of printer`);
                if (autoCleanAtBootEnabled) {
                    await this.cleanPrinterFiles(printer.id);
                }
            }
        } finally{
            this.logger.log(`Printer old file analysis job ended`);
        }
    }
    async cleanPrinterFiles(printerId) {
        await this.printerFilesStore.deleteOutdatedFiles(printerId, this.ageDaysMaxSetting);
        await this.printerFilesStore.loadFiles(printerId, false);
    }
    getPrinterOutdatedFiles(printer) {
        const ageDaysMax = this.ageDaysMaxSetting;
        return this.printerFilesStore.getOutdatedFiles(printer.id, ageDaysMax);
    }
    getSettings() {
        return this.settingsStore.getFileCleanSettings();
    }
}

//# sourceMappingURL=printer-file-clean.task.js.map