var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useAppSettings = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = require("react");
var _useIsMountedRef = require("../../../hooks/useIsMountedRef");
var useAppSettings = exports.useAppSettings = function useAppSettings(client, isOnline, enableOfflineSupport, initialisedDatabase) {
  var _useState = (0, _react.useState)(null),
    _useState2 = (0, _slicedToArray2["default"])(_useState, 2),
    appSettings = _useState2[0],
    setAppSettings = _useState2[1];
  var appSettingsPromise = (0, _react.useRef)(null);
  var fetchedAppSettings = (0, _react.useRef)(false);
  var isMounted = (0, _useIsMountedRef.useIsMountedRef)();
  (0, _react.useEffect)(function () {
    if (fetchedAppSettings.current) {
      return;
    }
    var fetchAppSettings = function fetchAppSettings() {
      if (appSettingsPromise.current) {
        return appSettingsPromise.current;
      }
      appSettingsPromise.current = client.getAppSettings();
      return appSettingsPromise.current;
    };
    var enforceAppSettings = function () {
      var _ref = (0, _asyncToGenerator2["default"])(_regenerator["default"].mark(function _callee() {
        var userId, _appSettings, _appSettings2, _client$offlineDb;
        return _regenerator["default"].wrap(function _callee$(_context) {
          while (1) switch (_context.prev = _context.next) {
            case 0:
              if (client.userID) {
                _context.next = 2;
                break;
              }
              return _context.abrupt("return");
            case 2:
              if (!(enableOfflineSupport && !initialisedDatabase)) {
                _context.next = 4;
                break;
              }
              return _context.abrupt("return");
            case 4:
              userId = client.userID;
              if (!(!isOnline && client.offlineDb)) {
                _context.next = 11;
                break;
              }
              _context.next = 8;
              return client.offlineDb.getAppSettings({
                userId: userId
              });
            case 8:
              _appSettings = _context.sent;
              setAppSettings(_appSettings);
              return _context.abrupt("return");
            case 11:
              _context.prev = 11;
              _context.next = 14;
              return fetchAppSettings();
            case 14:
              _appSettings2 = _context.sent;
              if (isMounted.current && _appSettings2) {
                setAppSettings(_appSettings2);
                fetchedAppSettings.current = true;
                (_client$offlineDb = client.offlineDb) == null || _client$offlineDb.executeQuerySafely(function (db) {
                  return db.upsertAppSettings({
                    appSettings: _appSettings2,
                    userId: userId
                  });
                }, {
                  method: 'upsertAppSettings'
                });
              }
              _context.next = 21;
              break;
            case 18:
              _context.prev = 18;
              _context.t0 = _context["catch"](11);
              if (_context.t0 instanceof Error) {
                console.error("An error occurred while getting app settings: ".concat(_context.t0));
              }
            case 21:
            case "end":
              return _context.stop();
          }
        }, _callee, null, [[11, 18]]);
      }));
      return function enforceAppSettings() {
        return _ref.apply(this, arguments);
      };
    }();
    enforceAppSettings();
  }, [client, isOnline, initialisedDatabase, isMounted, enableOfflineSupport]);
  return appSettings;
};
//# sourceMappingURL=useAppSettings.js.map