'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 _customError = require('../lib/custom-error');

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

var _questionnaire = require('./questionnaire');

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];

  switch (action.type) {
    case types.FETCH_SECTIONS_FEATURED_SUCCESS:
      var update = {};
      for (var key in action.response) {
        if (action.response) {
          _lodash2.default.merge(update, action['response'][key]['entities']);
        }
      }
      return _lodash2.default.merge({}, state, update);
    default:
      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,
  audios: _articles.audios,
  categories: _groups.categories,
  choices: _articles.choices,
  device: _device2.default,
  entities: entities,
  event: _articles.event,
  fatalError: fatalError,
  featureArticles: _articles.featureArticles,
  header: _header2.default,
  images: _articles.images,
  indexArticles: _articles.indexArticles,
  latestPosts: _articles.latestPosts,
  questionnaire: _questionnaire.questionnaire,
  questSetting: _questionnaire.questSetting,
  relatedArticles: _articles.relatedArticles,
  routing: _reactRouterRedux.routerReducer,
  searchResult: _articles.searchResult,
  sectionFeatured: _articles.sectionFeatured,
  sectionList: _articles.sectionList,
  selectedArticle: _article2.default,
  slugToId: slugToId,
  tags: _groups.tags,
  topic: _articles.topic,
  topics: _articles.topics,
  twitterTimeline: _articles.twitterTimeline,
  youtubePlaylist: _articles.youtubePlaylist
});

exports.default = rootReducer;