import { DEFAULT_CACHING_SAFE_LAG, DEFAULT_CACHING_TTL } from "../constants";
import assert from "assert";
import { composeRevivers, objectWithBigNumberReviver } from "./ReviverUtils";
import { getRelayEventKey } from "./DepositUtils";
import { getCurrentTime } from "./TimeUtils";
import { isDefined } from "./TypeGuards";
export function shouldCache(eventTimestamp, latestTime, cachingMaxAge) {
    assert(eventTimestamp.toString().length === 10, "eventTimestamp must be in seconds");
    assert(latestTime.toString().length === 10, "eventTimestamp must be in seconds");
    return latestTime - eventTimestamp >= cachingMaxAge;
}
/**
 * Calls the cache's get method and returns the result if it is defined, otherwise returns undefined.
 * @param key The key to get from the cache.
 * @param cache The cache to get the key from.
 * @returns The value associated with the key in the cache, or undefined if the key is not in the cache.
 */
export async function getDepositInCache(key, cache) {
    const depositRaw = await cache.get(key);
    return isDefined(depositRaw) ? JSON.parse(depositRaw, composeRevivers(objectWithBigNumberReviver)) : undefined;
}
export async function setDepositInCache(deposit, currentChainTime, cache, expirySeconds = DEFAULT_CACHING_TTL, timeToCache = DEFAULT_CACHING_SAFE_LAG) {
    const currentTimeInSeconds = getCurrentTime();
    // We should first confirm that neither the deposit's quoteTimestamp nor the currentChainTime
    // are in the future. If they are, we should not cache the deposit.
    if (deposit.quoteTimestamp > currentTimeInSeconds || currentChainTime > currentTimeInSeconds) {
        return;
    }
    // We should note here that the user can theoretically set the deposit's quoteTimestamp
    // to whatever they want. As a result, this could be used to manipulate the caching mechanism.
    if (shouldCache(deposit.quoteTimestamp, currentChainTime, timeToCache)) {
        await cache.set(getDepositKey(deposit), JSON.stringify(deposit), expirySeconds);
    }
}
/**
 * Resolves the key for caching a bridge event.
 * @param bridgeEvent The depositId, and originChainId are used to generate the key for v2, and the
 * full V3 relay hash is used for v3 events..
 * @returns The key for caching the event.
 */
export function getDepositKey(bridgeEvent) {
    const relayHash = getRelayEventKey(bridgeEvent);
    return `deposit_${bridgeEvent.originChainId}_${bridgeEvent.depositId.toString()}_${relayHash}`;
}
export function getArweaveTopicCacheKey(tag) {
    return `arweave-topic:${tag}`;
}
//# sourceMappingURL=CachingUtils.js.map