"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.shouldCache = shouldCache;
exports.getDepositInCache = getDepositInCache;
exports.setDepositInCache = setDepositInCache;
exports.getDepositKey = getDepositKey;
exports.getArweaveTopicCacheKey = getArweaveTopicCacheKey;
const tslib_1 = require("tslib");
const constants_1 = require("../constants");
const assert_1 = tslib_1.__importDefault(require("assert"));
const ReviverUtils_1 = require("./ReviverUtils");
const DepositUtils_1 = require("./DepositUtils");
const TimeUtils_1 = require("./TimeUtils");
const TypeGuards_1 = require("./TypeGuards");
function shouldCache(eventTimestamp, latestTime, cachingMaxAge) {
    (0, assert_1.default)(eventTimestamp.toString().length === 10, "eventTimestamp must be in seconds");
    (0, assert_1.default)(latestTime.toString().length === 10, "eventTimestamp must be in seconds");
    return latestTime - eventTimestamp >= cachingMaxAge;
}
async function getDepositInCache(key, cache) {
    const depositRaw = await cache.get(key);
    return (0, TypeGuards_1.isDefined)(depositRaw) ? JSON.parse(depositRaw, (0, ReviverUtils_1.composeRevivers)(ReviverUtils_1.objectWithBigNumberReviver)) : undefined;
}
async function setDepositInCache(deposit, currentChainTime, cache, expirySeconds = constants_1.DEFAULT_CACHING_TTL, timeToCache = constants_1.DEFAULT_CACHING_SAFE_LAG) {
    const currentTimeInSeconds = (0, TimeUtils_1.getCurrentTime)();
    if (deposit.quoteTimestamp > currentTimeInSeconds || currentChainTime > currentTimeInSeconds) {
        return;
    }
    if (shouldCache(deposit.quoteTimestamp, currentChainTime, timeToCache)) {
        await cache.set(getDepositKey(deposit), JSON.stringify(deposit), expirySeconds);
    }
}
function getDepositKey(bridgeEvent) {
    const relayHash = (0, DepositUtils_1.getRelayEventKey)(bridgeEvent);
    return `deposit_${bridgeEvent.originChainId}_${bridgeEvent.depositId.toString()}_${relayHash}`;
}
function getArweaveTopicCacheKey(tag) {
    return `arweave-topic:${tag}`;
}
//# sourceMappingURL=CachingUtils.js.map