'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _asyncToGenerator = require('@babel/runtime/helpers/asyncToGenerator');
var _regeneratorRuntime = require('@babel/runtime/regenerator');
require('core-js/modules/es.array.concat.js');
require('core-js/modules/es.object.to-string.js');
require('core-js/modules/es.promise.js');
var uiresources_fixture = require('./uiresources.fixture.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var _asyncToGenerator__default = /*#__PURE__*/_interopDefault(_asyncToGenerator);
var _regeneratorRuntime__default = /*#__PURE__*/_interopDefault(_regeneratorRuntime);

var defaultFetchApi = /*#__PURE__*/function () {
  var _ref = _asyncToGenerator__default.default(/*#__PURE__*/_regeneratorRuntime__default.default.mark(function _callee(method, url, body, headers, testResponse) {
    return _regeneratorRuntime__default.default.wrap(function _callee$(_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          return _context.abrupt("return", testResponse || fetch(url, {
            method: method,
            credentials: 'include',
            headers: headers || {
              'Content-Type': 'application/json'
            },
            body: JSON.stringify(body)
          }).then(function (res) {
            return res.json();
          }).then(function (resJson) {
            // Don't return any data if an error happened (401, 404, 409, etc)
            // istanbul ignore next
            if (resJson.error || resJson.exception) {
              return null;
            }
            return resJson;
          }));
        case 1:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
  return function defaultFetchApi(_x, _x2, _x3, _x4, _x5) {
    return _ref.apply(this, arguments);
  };
}();
var getUiResourcesData = /*#__PURE__*/function () {
  var _ref3 = _asyncToGenerator__default.default(/*#__PURE__*/_regeneratorRuntime__default.default.mark(function _callee2(_ref2) {
    var baseApiUrl, lang, surveyId, appId, workspaceId, useCache, fetchApi, isTest, fetcher, api, langParam, surveyIdParam, appIdParam, workspaceIdParam, cacheParam;
    return _regeneratorRuntime__default.default.wrap(function _callee2$(_context2) {
      while (1) switch (_context2.prev = _context2.next) {
        case 0:
          baseApiUrl = _ref2.baseApiUrl, lang = _ref2.lang, surveyId = _ref2.surveyId, appId = _ref2.appId, workspaceId = _ref2.workspaceId, useCache = _ref2.useCache, fetchApi = _ref2.fetchApi, isTest = _ref2.isTest;
          // istanbul ignore next
          fetcher = fetchApi !== null && fetchApi !== void 0 ? fetchApi : defaultFetchApi;
          api = function api(method, path, body, headers) {
            return fetcher(method, "".concat(baseApiUrl).concat(path), body, headers,
            // istanbul ignore next
            isTest ? uiresources_fixture[path.split('?')[0]] : null);
          };
          langParam = "&lang=".concat(encodeURIComponent(lang !== null && lang !== void 0 ? lang : 'en'));
          surveyIdParam = surveyId ? "&surveyId=".concat(encodeURIComponent(surveyId)) : '';
          appIdParam = appId ? "&appId=".concat(encodeURIComponent(appId)) : '';
          workspaceIdParam = workspaceId ? "&workspaceId=".concat(encodeURIComponent(workspaceId)) : '';
          cacheParam = useCache ? "&cache=".concat(true) : '';
          return _context2.abrupt("return", api('GET', "/uiresources?id=masthead".concat(langParam).concat(surveyIdParam).concat(appIdParam).concat(workspaceIdParam).concat(cacheParam)));
        case 9:
        case "end":
          return _context2.stop();
      }
    }, _callee2);
  }));
  return function getUiResourcesData(_x6) {
    return _ref3.apply(this, arguments);
  };
}();
var getUiResourcesData$1 = getUiResourcesData;

exports.default = getUiResourcesData$1;
exports.defaultFetchApi = defaultFetchApi;
