"use strict";

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

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

var _store = _interopRequireDefault(require("store"));

var _constants = require("@instructure/quiz-common/lib/constants");

var _sessionStore = _interopRequireDefault(require("../util/sessionStore"));

/**
 * Some of our redux store values are stored in browser client-side storage
 * for easy recall in the event of a page refresh, etc. This provides a single
 * place to save all of those values so we can know how our data is organized
 * in client-side storage.
 *
 * See also src/common/util/persistedState.js for how these values are loaded.
 */
var persistTakingInfo = function persistTakingInfo(store, persistedLocalStore) {
  var position = store.getState().getIn(['taking', 'currentSessionItemPosition']);
  var sessionId = store.getState().getIn(['quizSessions', 'activeQuizSessionId']);
  persistedLocalStore.set(_constants.ACTIVE_QUESTION, position);
  persistedLocalStore.set(_constants.CACHED_SESSION_ID, sessionId);
};

var persistedStorageMiddleware = function persistedStorageMiddleware() {
  var persistedSessionStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _sessionStore.default;
  var persistedLocalStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : _store.default;
  return function (store) {
    return function (next) {
      return function (action) {
        var nextState = next(action);

        if (persistedLocalStore.enabled) {
          switch (action.type) {
            case _constants.ACTIVE_QUIZ_SESSION_CHANGE:
            case _constants.BACKTRACKING_NEXT_QUESTION:
            case _constants.PREVIOUS_QUESTION:
            case _constants.SET_CURRENT_SESSION_ITEM_POSITION:
              persistTakingInfo(store, persistedLocalStore);
              break;
          }
        }

        return nextState;
      };
    };
  };
};

var _default = persistedStorageMiddleware;
exports.default = _default;