"use strict";

exports.__esModule = true;
exports.resetConfig = exports.loadConfig = exports.buildUrl = exports.buildExternalAppUrl = void 0;
var _constants = require("../constants");
(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();
(function () {
  var enterModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.enterModule : undefined;
  enterModule && enterModule(module);
})();
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};
var __signature__ = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default.signature : function (a) {
  return a;
};
/* Constructs a URL search string from an object with key value pairs */
var buildParamString = function buildParamString(params) {
  return Object.entries(params).map(function (_ref) {
    var _ref2 = _slicedToArray(_ref, 2),
      key = _ref2[0],
      value = _ref2[1];
    return Array.isArray(value) ? "".concat(key, "=").concat(JSON.stringify(value)) : "".concat(key, "=").concat(value);
  }).join("&");
};

/* Placeholder until config loads */
var placeholder = function placeholder() {
  throw new Error("Config not yet loaded. Please call util.js#loadConfig() and await resolution of the returned Promise.");
};
var buildUrl = exports.buildUrl = placeholder;

/* Loads the local /config.json and sets up a real buildUrl() function 
from the acquired information */
var loadConfig = exports.loadConfig = function loadConfig() {
  return fetch("/config.json").then(function (response) {
    return response.json();
  }).catch(function () {
    console.warn("Failed to load config.json, falling back to dev defaults");
    return {
      serviceApiUrl: "https://occ-dev-ocs-cm.develop.orckestra.cloud:443/api"
    };
  }).then(function (config) {
    var host = config.serviceApiUrl;
    /* Constructs a URL path with search parameters from a list of
    path segments and a key-value object. */
    exports.buildUrl = buildUrl = function buildUrl(pathParts, parameters) {
      if (pathParts === void 0) {
        pathParts = [];
      }
      return "".concat(host, "/").concat(pathParts.join("/")) + (parameters ? "?" + buildParamString(parameters) : "");
    };
  });
};
var buildExternalAppUrl = exports.buildExternalAppUrl = function buildExternalAppUrl(app, relativeUrl) {
  var url = relativeUrl[0] === "/" ? relativeUrl.substring(1) : relativeUrl;
  var lowercaseApp = app.toLowerCase();
  switch (lowercaseApp) {
    case _constants.applications.oms:
    case _constants.applications.pim:
      return "/".concat(lowercaseApp, "/app/").concat(url);
    default:
      throw new Error("Not implemented app '" + app + "'");
  }
};

/* Reset function for testing, never use this in actual code */
var resetConfig = exports.resetConfig = function resetConfig() {
  exports.buildUrl = buildUrl = placeholder;
};
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(buildParamString, "buildParamString", "/home/vsts/work/1/s/src/utils/buildUrl.js");
  reactHotLoader.register(placeholder, "placeholder", "/home/vsts/work/1/s/src/utils/buildUrl.js");
  reactHotLoader.register(buildUrl, "buildUrl", "/home/vsts/work/1/s/src/utils/buildUrl.js");
  reactHotLoader.register(loadConfig, "loadConfig", "/home/vsts/work/1/s/src/utils/buildUrl.js");
  reactHotLoader.register(buildExternalAppUrl, "buildExternalAppUrl", "/home/vsts/work/1/s/src/utils/buildUrl.js");
  reactHotLoader.register(resetConfig, "resetConfig", "/home/vsts/work/1/s/src/utils/buildUrl.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();
;
(function () {
  var reactHotLoader = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.default : undefined;
  if (!reactHotLoader) {
    return;
  }
  reactHotLoader.register(buildParamString, "buildParamString", "/home/vsts/work/1/s/src/utils/buildUrl.js");
  reactHotLoader.register(placeholder, "placeholder", "/home/vsts/work/1/s/src/utils/buildUrl.js");
  reactHotLoader.register(buildUrl, "buildUrl", "/home/vsts/work/1/s/src/utils/buildUrl.js");
  reactHotLoader.register(loadConfig, "loadConfig", "/home/vsts/work/1/s/src/utils/buildUrl.js");
  reactHotLoader.register(buildExternalAppUrl, "buildExternalAppUrl", "/home/vsts/work/1/s/src/utils/buildUrl.js");
  reactHotLoader.register(resetConfig, "resetConfig", "/home/vsts/work/1/s/src/utils/buildUrl.js");
})();
;
(function () {
  var leaveModule = typeof reactHotLoaderGlobal !== 'undefined' ? reactHotLoaderGlobal.leaveModule : undefined;
  leaveModule && leaveModule(module);
})();