"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get calculateJobDuration () {
        return calculateJobDuration;
    },
    get initializeOrUpdateStatistics () {
        return initializeOrUpdateStatistics;
    },
    get updateStatisticsForCancellation () {
        return updateStatisticsForCancellation;
    },
    get updateStatisticsForCompletion () {
        return updateStatisticsForCompletion;
    },
    get updateStatisticsForFailure () {
        return updateStatisticsForFailure;
    }
});
function calculateJobDuration(startedAt, endedAt = new Date()) {
    if (!startedAt) {
        return null;
    }
    return (endedAt.getTime() - startedAt.getTime()) / 1000;
}
function initializeOrUpdateStatistics(job, endedAt = new Date(), options = {}) {
    const actualPrintTimeSeconds = calculateJobDuration(job.startedAt, endedAt);
    if (!job.statistics) {
        return {
            startedAt: job.startedAt,
            endedAt,
            actualPrintTimeSeconds,
            progress: options.progress ?? job.progress ?? null,
            failureReason: options.failureReason,
            failureTime: options.failureTime
        };
    } else {
        job.statistics.endedAt = endedAt;
        job.statistics.actualPrintTimeSeconds = actualPrintTimeSeconds;
        if (options.progress !== undefined) {
            job.statistics.progress = options.progress;
        }
        if (options.failureReason !== undefined) {
            job.statistics.failureReason = options.failureReason;
            job.statistics.failureTime = options.failureTime ?? endedAt;
        }
        return job.statistics;
    }
}
function updateStatisticsForCompletion(job, endedAt = new Date()) {
    job.statistics = initializeOrUpdateStatistics(job, endedAt, {
        progress: 100
    });
    job.endedAt = endedAt;
    job.progress = 100;
}
function updateStatisticsForFailure(job, reason, endedAt = new Date()) {
    job.statistics = initializeOrUpdateStatistics(job, endedAt, {
        failureReason: reason,
        failureTime: endedAt
    });
    job.endedAt = endedAt;
    job.statusReason = reason;
}
function updateStatisticsForCancellation(job, reason = "Print cancelled by user", endedAt = new Date()) {
    job.statistics = initializeOrUpdateStatistics(job, endedAt);
    job.endedAt = endedAt;
    job.statusReason = reason;
}

//# sourceMappingURL=job-stats.util.js.map