/**
 *
 * @param exp JWT Token expire time
 * @param iat JWT Token create time
 * @param expTimeTolerance this constant tell when the token near expire or not
 *
 * this function will return true when the rest time (exp - now)
 * is less than expire duration (exp - iat) * expTimeTolerance
 * otherwise will return false
 */
export function isTokenNearExpired(
  exp: number,
  iat: number,
  expTimeTolerance = 0.2,
): boolean {
  const now = new Date().getTime() / 1000;
  const expDuration = exp - iat;
  const toleranceDuration = expDuration * expTimeTolerance;
  const restDuration = exp - now;

  return restDuration < toleranceDuration;
}
