'use strict';

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

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
 * 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 = exports.SessionStore = function () {
  function SessionStore(storage) {
    (0, _classCallCheck3.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, _createClass3.default)(SessionStore, [{
    key: 'checkStorageEnabled',
    value: function checkStorageEnabled() {
      if (!this.storage) {
        return false;
      }
      var testKey = 'storageCheck';
      var testValue = 'just checking';
      var result = void 0;
      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.default = new SessionStore();