"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.refreshToken = exports.UserProfile = exports.UserPrivileges = void 0;
var _tokenService = require("./tokenService");
var _axiosAPIClient = _interopRequireDefault(require("../api/axiosAPIClient"));
var _wrapperService = require("./wrapperService");
const BASE_AUTH_API_URL = process.env.REACT_APP_AUTHAPI_URL;
const refreshToken = async token => {
  try {
    if ((0, _tokenService.isGivenTokenEmpty)(token)) {
      token = (0, _tokenService.getStorageToken)();
    }
    const response = await _axiosAPIClient.default.post("".concat(BASE_AUTH_API_URL).concat((0, _wrapperService.getAPIPrefix)(), "/Account/RenewToken?jwtToken=").concat(token));
    if (response.status === 200) {
      const json = await response.data;
      (0, _tokenService.setStorageToken)(json);
      return json;
    } else {
      (0, _tokenService.setStorageToken)("");
      return "";
    }
  } catch (err) {
    console.log("err " + err);
    throw new Error("Invalid User");
  }
};
exports.refreshToken = refreshToken;
const UserPrivileges = async userID => {
  try {
    const privilegesRes = await _axiosAPIClient.default.post("".concat(BASE_AUTH_API_URL, "api/").concat((0, _wrapperService.getAPIPrefix)(), "/UserManagement/GetApplicationPrivileges/"), {
      securityAppName: "RAP",
      userID: userID
    });
    if (privilegesRes.status === 200) {
      return await privilegesRes.data;
    }
  } catch (err) {
    console.log("err " + err);
    throw new Error("Invalid User");
  }
};
exports.UserPrivileges = UserPrivileges;
const UserProfile = async emailID => {
  try {
    const response = await _axiosAPIClient.default.get("".concat(BASE_AUTH_API_URL, "api/").concat((0, _wrapperService.getAPIPrefix)(), "/account/GetUserDetails?emailid=").concat(emailID));
    if (response.status === 200) {
      const userinfo = await response.data;
      return userinfo;
    }
  } catch (err) {
    console.log("err " + err);
    throw new Error("Invalid User");
  }
};
exports.UserProfile = UserProfile;