'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };

var _articles = require('./articles');

var _groups = require('./groups');

var _redux = require('redux');

var _reactRouterRedux = require('react-router-redux');

var _customError = require('../lib/custom-error');

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _actionTypes = require('../constants/action-types');

var types = _interopRequireWildcard(_actionTypes);

var _device = require('./device');

var _device2 = _interopRequireDefault(_device);

var _header = require('./header');

var _header2 = _interopRequireDefault(_header);

var _article = require('./article');

var _article2 = _interopRequireDefault(_article);

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

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

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

// Updates an entity cache in response to any action with response.entities.
function entities() {
  var state = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
  var action = arguments[1];

  if (action.response && action.response.entities) {
    return _lodash2.default.merge({}, state, action.response.entities);
  }
  return state;
}

function slugToId() {
  var state = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
  var action = arguments[1];

  var _ret = function () {
    switch (action.type) {
      case types.FETCH_ARTICLE_SUCCESS:
        return {
          v: _lodash2.default.merge({}, state, _defineProperty({}, action.slug, _lodash2.default.get(action, 'response.result')))
        };
      case types.FETCH_ARTICLES_BY_GROUP_UUID_SUCCESS:
      case types.FETCH_FEATURE_ARTICLES_SUCCESS:
      case types.FETCH_RELATED_ARTICLES_SUCCESS:
        var rtn = {};
        var articles = _lodash2.default.get(action, ['response', 'entities', 'articles'], {});
        _lodash2.default.forEach(articles, function (article) {
          rtn[article.slug] = article.id;
        });
        return {
          v: _lodash2.default.merge({}, state, rtn)
        };
      default:
        return {
          v: state
        };
    }
  }();

  if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
}

function fatalError() {
  var state = arguments.length <= 0 || arguments[0] === undefined ? null : arguments[0];
  var action = arguments[1];

  switch (action.type) {
    case types.FETCH_ARTICLE_FAILURE:
      var error = action.error;

      if (error instanceof _customError.FatalError) {
        return error;
      }
      return state;
    default:
      return state;
  }
}

var rootReducer = (0, _redux.combineReducers)({
  articlesByUuids: _articles.articlesByUuids,
  featureArticles: _articles.featureArticles,
  relatedArticles: _articles.relatedArticles,
  categories: _groups.categories,
  fatalError: fatalError,
  device: _device2.default,
  selectedArticle: _article2.default,
  tags: _groups.tags,
  routing: _reactRouterRedux.routerReducer,
  header: _header2.default,
  slugToId: slugToId,
  entities: entities
});

exports.default = rootReducer;