var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
var _CoreManager = _interopRequireDefault(require("./CoreManager"));
var _isRevocableSession = _interopRequireDefault(require("./isRevocableSession"));
var _ParseObject2 = _interopRequireDefault(require("./ParseObject"));
var _ParseUser = _interopRequireDefault(require("./ParseUser"));
function _callSuper(t, o, e) {
  return o = (0, _getPrototypeOf2.default)(o), (0, _possibleConstructorReturn2.default)(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2.default)(t).constructor) : o.apply(t, e));
}
function _isNativeReflectConstruct() {
  try {
    var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
  } catch (t) {}
  return (_isNativeReflectConstruct = function () {
    return !!t;
  })();
}
var ParseSession = function (_ParseObject) {
  function ParseSession(attributes) {
    var _this;
    (0, _classCallCheck2.default)(this, ParseSession);
    _this = _callSuper(this, ParseSession, ['_Session']);
    if (attributes && typeof attributes === 'object') {
      if (!_this.set(attributes || {})) {
        throw new Error("Can't create an invalid Session");
      }
    }
    return _this;
  }
  (0, _inherits2.default)(ParseSession, _ParseObject);
  return (0, _createClass2.default)(ParseSession, [{
    key: "getSessionToken",
    value: function () {
      var token = this.get('sessionToken');
      if (typeof token === 'string') {
        return token;
      }
      return '';
    }
  }], [{
    key: "readOnlyAttributes",
    value: function () {
      return ['createdWith', 'expiresAt', 'installationId', 'restricted', 'sessionToken', 'user'];
    }
  }, {
    key: "current",
    value: function (options) {
      options = options || {};
      var controller = _CoreManager.default.getSessionController();
      var sessionOptions = {};
      if (options.hasOwnProperty('useMasterKey')) {
        sessionOptions.useMasterKey = options.useMasterKey;
      }
      return _ParseUser.default.currentAsync().then(function (user) {
        if (!user) {
          return Promise.reject('There is no current user.');
        }
        sessionOptions.sessionToken = user.getSessionToken();
        return controller.getSession(sessionOptions);
      });
    }
  }, {
    key: "isCurrentSessionRevocable",
    value: function () {
      var currentUser = _ParseUser.default.current();
      if (currentUser) {
        return (0, _isRevocableSession.default)(currentUser.getSessionToken() || '');
      }
      return false;
    }
  }]);
}(_ParseObject2.default);
_ParseObject2.default.registerSubclass('_Session', ParseSession);
var DefaultController = {
  getSession: function (options) {
    var RESTController = _CoreManager.default.getRESTController();
    var session = new ParseSession();
    return RESTController.request('GET', 'sessions/me', {}, options).then(function (sessionData) {
      session._finishFetch(sessionData);
      session._setExisted(true);
      return session;
    });
  }
};
_CoreManager.default.setSessionController(DefaultController);
var _default = exports.default = ParseSession;