"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resetAllLimits = resetAllLimits;
exports.setMaxLimit = setMaxLimit;
exports.incLimitedValue = incLimitedValue;
exports.isLimitReached = isLimitReached;
const logger_1 = require("../../logger");
const limits = new Map();
function resetAllLimits() {
    limits.clear();
}
function setMaxLimit(key, val) {
    const max = typeof val === 'number' ? Math.max(0, val) : null;
    limits.set(key, { current: 0, max });
    logger_1.logger.debug(`${key} limit = ${max}`);
}
function incLimitedValue(key, incBy = 1) {
    const limit = limits.get(key) ?? { max: null, current: 0 };
    limits.set(key, {
        ...limit,
        current: limit.current + incBy,
    });
}
function isLimitReached(key) {
    const limit = limits.get(key);
    // TODO: fix me?
    // eslint-disable-next-line @typescript-eslint/prefer-optional-chain
    if (!limit || limit.max === null) {
        return false;
    }
    const { max, current } = limit;
    return max - current <= 0;
}
//# sourceMappingURL=limits.js.map