'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.reducerFactory = reducerFactory;
exports.onGameResponse = onGameResponse;
exports.onGameRequest = onGameRequest;
exports.onConfigChange = onConfigChange;

var _redux = require('redux');

var _lodash = require('lodash');

var _const = require('./const');

var _info = require('./info');

var _sortie = require('./sortie');

var _timers = require('./timers');

var _config = require('./config');

var _layout = require('./layout');

var _battle = require('./battle');

var _plugins = require('./plugins');

var _fcd = require('./fcd');

var _ui = require('./ui');

var _ipc = require('./ipc');

var _misc = require('./misc');

var _misc2 = _interopRequireDefault(_misc);

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

const emptyObject = {};

// === Utils ===

// === Root reducer ===

function _ref() {
  return emptyObject;
}

function _ref2(func, key) {
  if (func) {
    // Use combineReducers to check sanity of `func`
    const wrappedReducer = (0, _redux.combineReducers)({ _: func });
    return (state = {}, action) => {
      try {
        return wrappedReducer(state, action);
      } catch (e) {
        console.error(`Error in extension ${key}`, e.stack);
        return state;
      }
    };
  } else {
    return _ref;
  }
}

function secureExtensionConfig(extensionConfig) {
  return (0, _lodash.mapValues)(extensionConfig, _ref2);
}

function _ref3() {
  return emptyObject;
}

function _ref4() {
  return emptyObject;
}

function _ref5() {
  return emptyObject;
}

function _ref6() {
  return emptyObject;
}

function reducerFactory(extensionConfig) {
  return (0, _redux.combineReducers)({
    const: _const.reducer,
    info: _info.reducer,
    sortie: _sortie.reducer,
    timers: _timers.reducer,
    config: _config.reducer,
    battle: _battle.reducer,
    misc: _misc2.default,
    fcd: _fcd.reducer,
    plugins: window.isMain ? _plugins.reducer : _ref3,
    layout: window.isMain ? _layout.reducer : _ref4,
    ui: window.isMain ? _ui.reducer : _ref5,
    ext: extensionConfig ? (0, _redux.combineReducers)(secureExtensionConfig(extensionConfig)) : _ref6,
    ipc: _ipc.reducer
  });
}

// === Actions ===

function onGameResponse({ method, path, body, postBody, time }) {
  return {
    type: `@@Response${path}`,
    method,
    path,
    body,
    postBody,
    time
  };
}

function onGameRequest({ method, path, body, time }) {
  return {
    type: `@@Request${path}`,
    method,
    path,
    body,
    time
  };
}

function onConfigChange({ path, value }) {
  return {
    type: `@@Config`,
    path,
    value
  };
}