"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "FileUploadTrackerCache", {
    enumerable: true,
    get: function() {
        return FileUploadTrackerCache;
    }
});
const _correlationtokenutil = require("../utils/correlation-token.util");
const _eventconstants = require("../constants/event.constants");
class FileUploadTrackerCache {
    currentUploads = [];
    uploadsDone = [];
    uploadsFailed = [];
    eventEmitter2;
    logger;
    constructor({ loggerFactory, eventEmitter2 }){
        this.eventEmitter2 = eventEmitter2;
        this.logger = loggerFactory(FileUploadTrackerCache.name);
    }
    progressCallback = (token, p)=>{
        this.updateUploadProgress(token, p);
    };
    getUploads(filterHourOld = false) {
        const currentTime = Date.now();
        const done = this.uploadsDone.filter((d)=>currentTime - d.startedAt < 60 * 60 * 1000);
        const failed = this.uploadsFailed.filter((d)=>currentTime - d.startedAt < 60 * 60 * 1000);
        return filterHourOld ? {
            current: this.currentUploads,
            done,
            failed
        } : {
            current: this.currentUploads,
            done: this.uploadsDone,
            failed: this.uploadsFailed
        };
    }
    getUpload(correlationToken) {
        return this.currentUploads.find((cu)=>cu.correlationToken === correlationToken);
    }
    addUploadTracker(multerFile) {
        const correlationToken = (0, _correlationtokenutil.generateCorrelationToken)();
        this.logger.log(`Starting upload session with token ${correlationToken}`);
        this.eventEmitter2.on((0, _eventconstants.uploadProgressEvent)(correlationToken), this.progressCallback);
        this.currentUploads.push({
            correlationToken,
            startedAt: Date.now(),
            multerFile,
            progress: {}
        });
        return correlationToken;
    }
    updateUploadProgress(token, progress, reason) {
        if (progress.progress === 1) {
            this.logger.log("Upload tracker completed");
            this.markUploadDone(token, true);
            this.eventEmitter2.off((0, _eventconstants.uploadProgressEvent)(token), this.progressCallback);
        } else if (progress.failed) {
            this.markUploadDone(token, false, reason);
            this.eventEmitter2.off((0, _eventconstants.uploadProgressEvent)(token), this.progressCallback);
        } else {
            const upload = this.getUpload(token);
            upload.progress = progress;
        }
    }
    markUploadDone(token, success, reason) {
        const trackedUploadIndex = this.currentUploads.findIndex((cu)=>cu.correlationToken === token);
        if (trackedUploadIndex === -1) {
            this.logger.warn(`Could not mark upload tracker with correlation token '${token}' as done as it was not found.`);
            return;
        }
        const trackedUpload = this.currentUploads[trackedUploadIndex];
        if (success) {
            trackedUpload.failedAt = Date.now();
            trackedUpload.reason = reason;
            this.uploadsDone.push(trackedUpload);
        } else {
            trackedUpload.succeededAt = Date.now();
            this.uploadsFailed.push(trackedUpload);
        }
        this.currentUploads.splice(trackedUploadIndex, 1);
    }
}

//# sourceMappingURL=file-upload-tracker.cache.js.map