"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.SessionStore = void 0;

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

/**
 * A store that behaves like https://github.com/marcuswestin/store.js but writes
 * to sessionStorage instead of localStorage, because some things you want to be
 * accessible to only a single browser window/tab.
 */
var SessionStore =
/*#__PURE__*/
function () {
  function SessionStore(storage) {
    (0, _classCallCheck2.default)(this, SessionStore);
    var newStorage = storage;

    if (newStorage === void 0) {
      // Chrome raises a DOMException when trying to access sessionStorage if
      // it has been disabled
      try {
        newStorage = window.sessionStorage;
      } catch (e) {// continue regardless of error
      }
    }

    this.storage = newStorage;
    this.enabled = this.checkStorageEnabled();
  }

  (0, _createClass2.default)(SessionStore, [{
    key: "checkStorageEnabled",
    value: function checkStorageEnabled() {
      if (!this.storage) {
        return false;
      }

      var testKey = 'storageCheck';
      var testValue = 'just checking';
      var result;

      try {
        this.set(testKey, testValue);
        result = this.get(testKey);
      } catch (e) {// continue regardless of error
      }

      return result === testValue;
    }
  }, {
    key: "get",
    value: function get(key) {
      return this.storage.getItem(key);
    }
  }, {
    key: "set",
    value: function set(key, value) {
      if (value) {
        return this.storage.setItem(key, value);
      } else {
        return this.remove(key);
      }
    }
  }, {
    key: "remove",
    value: function remove(key) {
      return this.storage.removeItem(key);
    }
  }]);
  return SessionStore;
}();

exports.SessionStore = SessionStore;

var _default = new SessionStore();

exports.default = _default;