"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.setStorageToken = exports.setInvalidStorageToken = exports.isStorageTokenInvalidOrEmpty = exports.isGivenTokenInvalid = exports.isGivenTokenExpiring = exports.isGivenTokenExpired = exports.isGivenTokenEmpty = exports.getStorageToken = void 0;
var _localstorageService = require("./localstorageService");
var _jwtDecode = _interopRequireDefault(require("jwt-decode"));
const invalidToken = "Invalid Token";
const setInvalidStorageToken = () => {
  (0, _localstorageService.setAccessToken)(invalidToken);
};
exports.setInvalidStorageToken = setInvalidStorageToken;
const isStorageTokenInvalidOrEmpty = () => {
  let token = getStorageToken();
  if (token === invalidToken) {
    return true;
  }
  if (isGivenTokenEmpty(token)) {
    return true;
  }
  return false;
};
exports.isStorageTokenInvalidOrEmpty = isStorageTokenInvalidOrEmpty;
const isGivenTokenInvalid = token => {
  if (token === invalidToken) {
    return true;
  }
  return false;
};
exports.isGivenTokenInvalid = isGivenTokenInvalid;
const isGivenTokenEmpty = token => {
  if (token === undefined || token === null || token === "" || token === "null") {
    return true;
  }
  return false;
};
exports.isGivenTokenEmpty = isGivenTokenEmpty;
const isGivenTokenExpired = token => {
  if (isGivenTokenEmpty(token) === false) {
    return calc(token, 0);
  }
  return undefined;
};
exports.isGivenTokenExpired = isGivenTokenExpired;
const isGivenTokenExpiring = token => {
  if (isGivenTokenEmpty(token) === false) {
    return calc(token, 5);
  }
  return undefined;
};
exports.isGivenTokenExpiring = isGivenTokenExpiring;
const calc = (token, diffMins) => {
  const decode = (0, _jwtDecode.default)(token);
  const current = Date.now() / 1000;
  const tokenExpiry = decode.exp;
  const secs = 60 * diffMins;
  if (tokenExpiry - current <= secs) {
    return true;
  }
  return false;
};
const getStorageToken = () => {
  return (0, _localstorageService.getAccessToken)();
};
exports.getStorageToken = getStorageToken;
const setStorageToken = token => {
  (0, _localstorageService.setAccessToken)(token);
};
exports.setStorageToken = setStorageToken;