"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _CoreManager = _interopRequireDefault(require("./CoreManager"));
var _Storage = _interopRequireDefault(require("./Storage"));
var _ParseInstallation = _interopRequireDefault(require("./ParseInstallation"));
var _uuid = _interopRequireDefault(require("./uuid"));
function _interopRequireDefault(e) {
  return e && e.__esModule ? e : {
    default: e
  };
}
const CURRENT_INSTALLATION_KEY = 'currentInstallation';
const CURRENT_INSTALLATION_ID_KEY = 'currentInstallationId';
let iidCache = null;
let currentInstallationCache = null;
let currentInstallationCacheMatchesDisk = false;
const InstallationController = {
  async updateInstallationOnDisk(installation) {
    const path = _Storage.default.generatePath(CURRENT_INSTALLATION_KEY);
    await _Storage.default.setItemAsync(path, JSON.stringify(installation.toJSON()));
    this._setCurrentInstallationCache(installation);
  },
  async currentInstallationId() {
    if (typeof iidCache === 'string') {
      return iidCache;
    }
    const path = _Storage.default.generatePath(CURRENT_INSTALLATION_ID_KEY);
    let iid = await _Storage.default.getItemAsync(path);
    if (!iid) {
      iid = (0, _uuid.default)();
      return _Storage.default.setItemAsync(path, iid).then(() => {
        iidCache = iid;
        return iid;
      });
    }
    iidCache = iid;
    return iid;
  },
  async currentInstallation() {
    if (currentInstallationCache) {
      return currentInstallationCache;
    }
    if (currentInstallationCacheMatchesDisk) {
      return null;
    }
    const path = _Storage.default.generatePath(CURRENT_INSTALLATION_KEY);
    let installationData = await _Storage.default.getItemAsync(path);
    currentInstallationCacheMatchesDisk = true;
    if (installationData) {
      installationData = JSON.parse(installationData);
      installationData.className = '_Installation';
      const current = _ParseInstallation.default.fromJSON(installationData);
      currentInstallationCache = current;
      return current;
    }
    const installationId = await this.currentInstallationId();
    const installation = new _ParseInstallation.default();
    installation.set('deviceType', _ParseInstallation.default.DEVICE_TYPES.WEB);
    installation.set('installationId', installationId);
    installation.set('parseVersion', _CoreManager.default.get('VERSION'));
    currentInstallationCache = installation;
    await _Storage.default.setItemAsync(path, JSON.stringify(installation.toJSON()));
    return installation;
  },
  _clearCache() {
    iidCache = null;
    currentInstallationCache = null;
    currentInstallationCacheMatchesDisk = false;
  },
  _setInstallationIdCache(iid) {
    iidCache = iid;
  },
  _setCurrentInstallationCache(installation, matchesDisk = true) {
    currentInstallationCache = installation;
    currentInstallationCacheMatchesDisk = matchesDisk;
  }
};
module.exports = InstallationController;
var _default = exports.default = InstallationController;