"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UpdateDelayLimiter = void 0;
const CoreToken_1 = require("@golemio/core/dist/helpers/ioc/CoreToken");
const Di_1 = require("@golemio/core/dist/integration-engine/ioc/Di");
/* Helper to decide whether initial message is older than the queue TTL, do not continue with processing it.
 * It is expected that older positions will be processed with more actual data.
 */
class UpdateDelayLimiter {
    constructor(name, queueTtl, logger) {
        this.name = name;
        this.queueTtl = queueTtl;
        this.skippedIterations = 0;
        this.logger = logger ?? Di_1.IntegrationEngineContainer.resolve(CoreToken_1.CoreToken.Logger);
    }
    isActualMessage(messageProperties) {
        if (!messageProperties) {
            this.logger.warn(`[UpdateDelayLimiter] Missing message properties for ${this.name}`);
            return true;
        }
        else if (!messageProperties.timestamp) {
            this.logger.warn(`[UpdateDelayLimiter] Missing message timestamp for ${this.name}`);
            return true;
        }
        else if (new Date().getTime() - messageProperties.timestamp > this.queueTtl) {
            this.skippedIterations++;
            if (this.skippedIterations > UpdateDelayLimiter.MAX_SKIPPED_ITERATIONS) {
                this.logger.warn(`[UpdateDelayLimiter] Skipped processing of update delay task ${this.skippedIterations} times.`);
                this.skippedIterations = 0;
            }
            return false;
        }
        return true;
    }
}
exports.UpdateDelayLimiter = UpdateDelayLimiter;
UpdateDelayLimiter.MAX_SKIPPED_ITERATIONS = 100;
//# sourceMappingURL=UpdateDelayLimiter.js.map