"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getApiUrl = getApiUrl;
exports.getApiUrl2 = getApiUrl2;
exports.getApiUrl3 = getApiUrl3;
exports.taker = taker;

var _bsGlobal = require("./bs-global");

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }

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

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 taker(_x, _x2, _x3) {
  return _taker.apply(this, arguments);
}

function _taker() {
  _taker = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(url, method, body) {
    var header,
        settings,
        bodyStr,
        key,
        response,
        data,
        _args = arguments;
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            header = _args.length > 3 && _args[3] !== undefined ? _args[3] : {};
            settings = {};

            if (method) {
              settings.headers = _objectSpread(_objectSpread({}, {
                Accept: "application/json, application/xml, text/play, text/html, *.*",
                "Content-Type": "application/json; charset=utf-8"
              }), header);
              settings.method = method;
            }

            if (body) settings.body = JSON.stringify(body);

            if (settings.headers) {
              if (settings.headers["Content-Type"] == "application/x-www-form-urlencoded") {
                bodyStr = "";

                for (key in body) {
                  bodyStr += "".concat(key, "=").concat(body[key], "&");
                }

                bodyStr.substring(0, bodyStr.length - 1);
                settings.body = bodyStr;
              }
            }

            settings.credentials = "same-origin";
            _context.prev = 6;
            _context.next = 9;
            return fetch(url, settings);

          case 9:
            response = _context.sent;
            _context.next = 12;
            return response.json();

          case 12:
            data = _context.sent;
            return _context.abrupt("return", data);

          case 16:
            _context.prev = 16;
            _context.t0 = _context["catch"](6);
            _context.next = 20;
            return Promise.reject(_context.t0);

          case 20:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, null, [[6, 16]]);
  }));
  return _taker.apply(this, arguments);
}

function getApiUrl(url) {
  var _getTenantInfo;

  var uid = (0, _bsGlobal.getUserInfo)().Id;
  var tid = (_getTenantInfo = (0, _bsGlobal.getTenantInfo)()) === null || _getTenantInfo === void 0 ? void 0 : _getTenantInfo.Id;
  var apiServer = (0, _bsGlobal.getBSGlobal)("apiPath");
  return "".concat(apiServer, "/api/v1/").concat(tid, "/").concat(uid, "/").concat(url);
}

function getApiUrl3(url) {
  var _getTenantInfo2;

  var uid = (0, _bsGlobal.getUserInfo)().Id;
  var tid = (_getTenantInfo2 = (0, _bsGlobal.getTenantInfo)()) === null || _getTenantInfo2 === void 0 ? void 0 : _getTenantInfo2.Id;
  var apiServer = (0, _bsGlobal.getBSGlobal)("apiPath");
  return "".concat(apiServer, "/api/v3/").concat(tid, "/").concat(uid, "/").concat(url);
}

function getApiUrl2(url) {
  var _getTenantInfo3;

  var uid = (0, _bsGlobal.getUserInfo)().Id;
  var tid = (_getTenantInfo3 = (0, _bsGlobal.getTenantInfo)()) === null || _getTenantInfo3 === void 0 ? void 0 : _getTenantInfo3.Id;
  var apiServer = (0, _bsGlobal.getBSGlobal)("apiPath");
  return "".concat(apiServer, "/api/v2/").concat(tid, "/").concat(uid, "/").concat(url);
}