import 'core-js/modules/es.symbol.js';
import 'core-js/modules/es.array.filter.js';
import 'core-js/modules/es.object.get-own-property-descriptor.js';
import 'core-js/modules/es.object.get-own-property-descriptors.js';
import 'core-js/modules/es.object.keys.js';
import 'core-js/modules/es.object.to-string.js';
import 'core-js/modules/web.dom-collections.for-each.js';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _asyncToGenerator from '@babel/runtime/helpers/asyncToGenerator';
import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
import _regeneratorRuntime from '@babel/runtime/regenerator';
import { useState, useCallback, useEffect } from 'react';
import getUiResourcesData, { defaultFetchApi } from '../util/uiresources.js';
import getCachedUiResourcesData from '../util/cacheduiresources.js';

function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var useUiResources = function useUiResources(_ref) {
  var baseApiUrl = _ref.baseApiUrl,
    _ref$lang = _ref.lang,
    lang = _ref$lang === void 0 ? 'en' : _ref$lang,
    _ref$surveyId = _ref.surveyId,
    surveyId = _ref$surveyId === void 0 ? null : _ref$surveyId,
    _ref$appId = _ref.appId,
    appId = _ref$appId === void 0 ? null : _ref$appId,
    _ref$workspaceId = _ref.workspaceId,
    workspaceId = _ref$workspaceId === void 0 ? null : _ref$workspaceId,
    _ref$fetchApi = _ref.fetchApi,
    fetchApi = _ref$fetchApi === void 0 ? defaultFetchApi : _ref$fetchApi,
    _ref$isTest = _ref.isTest,
    isTest = _ref$isTest === void 0 ? false : _ref$isTest,
    _ref$useCache = _ref.useCache,
    useCache = _ref$useCache === void 0 ? true : _ref$useCache;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    isLoading = _useState2[0],
    setIsLoading = _useState2[1];
  var _useState3 = useState(),
    _useState4 = _slicedToArray(_useState3, 2),
    error = _useState4[0],
    setError = _useState4[1];
  var _useState5 = useState({
      username: null,
      userDisplayName: null,
      routes: null,
      applications: null,
      workspaces: null,
      globalApplications: null,
      i18n: null,
      surveyData: null,
      idleTimeoutData: null
    }),
    _useState6 = _slicedToArray(_useState5, 2),
    data = _useState6[0],
    setData = _useState6[1];
  var refreshData = useCallback(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    var options, uiResourcesData;
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          options = {
            baseApiUrl: baseApiUrl,
            lang: lang,
            surveyId: surveyId,
            appId: appId,
            workspaceId: workspaceId,
            fetchApi: fetchApi,
            isTest: isTest
          };
          _context.prev = 1;
          setIsLoading(true);
          if (!useCache) {
            _context.next = 8;
            break;
          }
          _context.next = 6;
          return getCachedUiResourcesData(_objectSpread({}, options), function (cachedData) {
            setData(cachedData);
            setIsLoading(false);
          }, function (err) {
            setError(err);
          });
        case 6:
          _context.next = 12;
          break;
        case 8:
          _context.next = 10;
          return getUiResourcesData(_objectSpread({}, options));
        case 10:
          uiResourcesData = _context.sent;
          setData(uiResourcesData);
        case 12:
          _context.next = 17;
          break;
        case 14:
          _context.prev = 14;
          _context.t0 = _context["catch"](1);
          setError(_context.t0);
        case 17:
          _context.prev = 17;
          setIsLoading(false);
          return _context.finish(17);
        case 20:
        case "end":
          return _context.stop();
      }
    }, _callee, null, [[1, 14, 17, 20]]);
  })), [baseApiUrl, lang, surveyId, appId, workspaceId, isTest, setIsLoading, useCache]); // eslint-disable-line react-hooks/exhaustive-deps

  useEffect(function () {
    // load actual data
    refreshData();
  }, [refreshData]);
  return [data, isLoading, error, refreshData];
};
var useUiResources$1 = useUiResources;

export { useUiResources$1 as default };
