"use strict";

require("core-js/modules/es.object.to-string.js");

require("core-js/modules/es.promise.js");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.TokenStore = void 0;

require("regenerator-runtime/runtime.js");

require("core-js/modules/es.date.to-string.js");

require("core-js/modules/es.array.concat.js");

require("core-js/modules/es.object.define-property.js");

var _serverConfig = require("./server-config");

var _axios = _interopRequireDefault(require("axios"));

var _jwtDecode = _interopRequireDefault(require("jwt-decode"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var TokenStore = /*#__PURE__*/function () {
  function TokenStore() {
    _classCallCheck(this, TokenStore);
  }

  _createClass(TokenStore, null, [{
    key: "setToken",
    value:
    /**
     * Set the token for the token store.
     *
     * @param tokenString
     */
    function setToken(tokenString) {
      if (tokenString === null) {
        this.token = tokenString;
        return;
      }

      var exp = (0, _jwtDecode["default"])(tokenString)["exp"];
      var expiration_date = new Date(exp * 1000);
      this.token = {
        tokenString: tokenString,
        exp: expiration_date
      };
    }
    /**
     * Get the auth token from the permission service
     */

  }, {
    key: "getToken",
    value: function () {
      var _getToken = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {
        var baseurl, path, token, response, tokenString, data;
        return regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                baseurl = _serverConfig.globalServerConfig.baseurl;
                path = "/service/accounts/user/token/";
                token = this.token; // If token is null or is expired try to get token from service

                if (!(!token || !token.tokenString || token.exp < new Date())) {
                  _context.next = 16;
                  break;
                }

                _context.prev = 4;
                _context.next = 7;
                return _axios["default"].get("".concat(baseurl).concat(path));

              case 7:
                response = _context.sent;
                data = response.data;
                tokenString = data ? data["token"] : null;
                _context.next = 15;
                break;

              case 12:
                _context.prev = 12;
                _context.t0 = _context["catch"](4);
                tokenString = null;

              case 15:
                // If not null save it to the store
                if (tokenString) {
                  this.setToken(tokenString);
                } else {
                  this.token = null;
                }

              case 16:
                if (!this.token) {
                  _context.next = 18;
                  break;
                }

                return _context.abrupt("return", this.token.tokenString);

              case 18:
                return _context.abrupt("return", null);

              case 19:
              case "end":
                return _context.stop();
            }
          }
        }, _callee, this, [[4, 12]]);
      }));

      function getToken() {
        return _getToken.apply(this, arguments);
      }

      return getToken;
    }()
  }]);

  return TokenStore;
}();

exports.TokenStore = TokenStore;

_defineProperty(TokenStore, "token", null);
//# sourceMappingURL=data:application/json;charset=utf-8;base64, 