"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.nonReduxDispatch = nonReduxDispatch;
exports.sweep = sweep;
var _difference = _interopRequireDefault(require("lodash/difference"));
var _omit = _interopRequireDefault(require("lodash/omit"));
var _stringifyJSON = require("./stringifyJSON");
var _actionTypes = require("../constants/actionTypes");
function sweep(state) {
  return {
    ...state,
    actionsById: (0, _omit.default)(state.actionsById, state.skippedActionIds),
    stagedActionIds: (0, _difference.default)(state.stagedActionIds, state.skippedActionIds),
    skippedActionIds: [],
    currentStateIndex: Math.min(state.currentStateIndex, state.stagedActionIds.length - 1)
  };
}
function nonReduxDispatch(store, message, instanceId, action, initialState, preInstances) {
  const instances = preInstances || store.getState().instances;
  const state = instances.states[instanceId];
  const options = instances.options[instanceId];
  if (message !== 'DISPATCH') {
    if (message === 'IMPORT') {
      if (options.features.import === true) {
        return (0, _stringifyJSON.stringifyJSON)(state.computedStates[state.currentStateIndex].state, true);
      }
      return initialState;
    }
    return undefined;
  }
  if (options.lib === 'redux') return undefined;
  switch (action.type) {
    case 'TOGGLE_ACTION':
      return (0, _stringifyJSON.stringifyJSON)(state, true);
    case 'JUMP_TO_STATE':
      return (0, _stringifyJSON.stringifyJSON)(state.computedStates[action.index].state, true);
    case 'JUMP_TO_ACTION':
      return (0, _stringifyJSON.stringifyJSON)(state.computedStates[state.stagedActionIds.indexOf(action.actionId)].state, true);
    case 'ROLLBACK':
      return (0, _stringifyJSON.stringifyJSON)(state.computedStates[0].state, true);
    case 'SWEEP':
      store.dispatch({
        type: _actionTypes.SET_STATE,
        newState: sweep(state)
      });
      return undefined;
    default:
      return undefined;
  }
}