"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.dispatchMonitorAction = dispatchMonitorAction;
exports.monitor = monitor;
var _reduxPersist = require("redux-persist");
var _actionTypes = require("../constants/actionTypes");
const initialState = {
  selected: 'InspectorMonitor',
  monitorState: undefined,
  sliderIsOpen: true,
  dispatcherIsOpen: false
};
function dispatchMonitorAction(state, action) {
  return {
    ...state,
    monitorState: action.action.newMonitorState || action.monitorReducer(action.monitorProps, state.monitorState, action.action)
  };
}
function monitor() {
  let state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : initialState;
  let action = arguments.length > 1 ? arguments[1] : undefined;
  switch (action.type) {
    case _actionTypes.MONITOR_ACTION:
      return dispatchMonitorAction(state, action);
    case _actionTypes.SELECT_MONITOR:
      {
        let monitorState = state.monitorState;
        if (action.monitorState) {
          monitorState = {
            ...action.monitorState,
            __overwritten__: action.monitor
          };
        }
        return {
          ...state,
          monitorState,
          selected: action.monitor
        };
      }
    case _actionTypes.UPDATE_MONITOR_STATE:
      {
        let inspectedStatePath = state.monitorState.inspectedStatePath;
        if (action.nextState.inspectedStatePath) {
          inspectedStatePath = [...inspectedStatePath.slice(0, -1), ...action.nextState.inspectedStatePath];
        }
        return {
          ...state,
          monitorState: {
            ...state.monitorState,
            ...action.nextState,
            inspectedStatePath
          }
        };
      }
    case _actionTypes.TOGGLE_SLIDER:
      return {
        ...state,
        sliderIsOpen: !state.sliderIsOpen
      };
    case _actionTypes.TOGGLE_DISPATCHER:
      return {
        ...state,
        dispatcherIsOpen: !state.dispatcherIsOpen
      };
    case _reduxPersist.REHYDRATE:
      {
        const rehydratedState = action.payload;
        if (!rehydratedState) return state;
        if (rehydratedState.monitor.monitorState && (typeof rehydratedState.monitor.monitorState.selectedActionId === 'number' || typeof rehydratedState.monitor.monitorState.startActionId === 'number')) {
          return {
            ...rehydratedState.monitor,
            monitorState: {
              ...rehydratedState.monitor.monitorState,
              selectedActionId: null,
              startActionId: null
            }
          };
        }
        return rehydratedState.monitor;
      }
    default:
      return state;
  }
}