"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.setAccessToken = setAccessToken;
exports.getAccessToken = getAccessToken;
exports.removeAccessToken = removeAccessToken;
exports.getUserInfo = getUserInfo;

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); }); }; }

var accessTokenKeyName = "access_token";

function setAccessToken(_x) {
  return _setAccessToken.apply(this, arguments);
}

function _setAccessToken() {
  _setAccessToken = _asyncToGenerator(
  /*#__PURE__*/
  regeneratorRuntime.mark(function _callee(_ref) {
    var authenCode, clientId, clientSecret, redirectUri, uri, body;
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            authenCode = _ref.authenCode, clientId = _ref.clientId, clientSecret = _ref.clientSecret, redirectUri = _ref.redirectUri;
            uri = process.env.REACT_APP_URI_TOKEN;
            body = "grant_type=authorization_code&code=".concat(authenCode, "&client_id=").concat(clientId, "&client_secret=").concat(clientSecret, "&redirect_uri=").concat(redirectUri);
            _context.next = 5;
            return fetch(uri, {
              method: 'POST',
              headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
              },
              body: body
            }).then(function (resp) {
              return resp.json();
            }).then(function (data) {
              localStorage.setItem(accessTokenKeyName, data.access_token);
            }).catch(function (error) {
              console.error(error);
            });

          case 5:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));
  return _setAccessToken.apply(this, arguments);
}

function getAccessToken() {
  var accessToken = localStorage.getItem(accessTokenKeyName);
  return accessToken;
}

function removeAccessToken() {
  localStorage.removeItem(accessTokenKeyName);
}

function getUserInfo(_x2) {
  return _getUserInfo.apply(this, arguments);
}

function _getUserInfo() {
  _getUserInfo = _asyncToGenerator(
  /*#__PURE__*/
  regeneratorRuntime.mark(function _callee2(accessToken) {
    var uri;
    return regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            uri = process.env.REACT_APP_URI_USERINFO;
            uri += '?access_token=' + accessToken;
            _context2.next = 4;
            return fetch(uri).then(function (data) {
              return data.json();
            }).then(function (userInfo) {
              return userInfo;
            }).catch(function (error) {
              return console.error(error);
            });

          case 4:
            return _context2.abrupt("return", _context2.sent);

          case 5:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  }));
  return _getUserInfo.apply(this, arguments);
}