const revokedTokens = new Map();

function removeExpiredTokens(nowSeconds) {
  for (const [tokenId, expiresAt] of revokedTokens.entries()) {
    if (expiresAt <= nowSeconds) {
      revokedTokens.delete(tokenId);
    }
  }
}

function revokeToken(payload) {
  if (!payload || !payload.jti || !payload.exp) {
    return;
  }

  revokedTokens.set(payload.jti, payload.exp);
}

function isTokenRevoked(payload, nowSeconds = Math.floor(Date.now() / 1000)) {
  removeExpiredTokens(nowSeconds);
  return Boolean(payload && payload.jti && revokedTokens.has(payload.jti));
}

module.exports = { isTokenRevoked, revokeToken };
