'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.relatedArticles = relatedArticles;
exports.articlesByUuids = articlesByUuids;
exports.sectionFeatured = sectionFeatured;
exports.choices = choices;
exports.latestPosts = latestPosts;
exports.event = event;
exports.topics = topics;
exports.topic = topic;
exports.images = images;
exports.audios = audios;
exports.sectionList = sectionList;
exports.indexArticles = indexArticles;
exports.searchResult = searchResult;
exports.youtubePlaylist = youtubePlaylist;
exports.twitterTimeline = twitterTimeline;
exports.featureArticles = featureArticles;

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

var types = _interopRequireWildcard(_actionTypes);

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

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

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 _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; }

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

  var _state = {};
  var id = action.id;
  switch (action.type) {
    case types.FETCH_ARTICLES_BY_GROUP_UUID_REQUEST:
    case types.FETCH_RELATED_ARTICLES_REQUEST:
      if (state.hasOwnProperty(id)) {
        _lodash2.default.merge(_state, {
          isFetching: true
        });
      } else {
        _lodash2.default.merge(_state, {
          isFetching: true,
          error: null,
          hasMore: false,
          items: []
        });
      }
      return _lodash2.default.merge({}, state, _defineProperty({}, id, _state));

    case types.FETCH_ARTICLES_BY_GROUP_UUID_FAILURE:
    case types.FETCH_RELATED_ARTICLES_FAILURE:
      _lodash2.default.merge(_state, {
        isFetching: false,
        error: action.error,
        lastUpdated: action.failedAt
      });
      return _lodash2.default.merge({}, state, _defineProperty({}, id, _state));

    case types.FETCH_RELATED_ARTICLES_SUCCESS:
      return _lodash2.default.merge({}, state, _defineProperty({}, id, {
        isFetching: false,
        hasMore: false,
        error: null,
        items: _lodash2.default.get(action, 'relatedIds'),
        total: _lodash2.default.get(action, 'relatedIds.length'),
        lastUpdated: action.receivedAt
      }));

    case types.FETCH_ARTICLES_BY_GROUP_UUID_SUCCESS:
      _state = _lodash2.default.get(state, id);
      var total = _lodash2.default.get(_state, 'total') || _lodash2.default.get(action, 'response.meta.total');
      var items = _lodash2.default.get(_state, 'items');
      var hasMore = _lodash2.default.get(_state, 'hasMore');

      // dedup items
      items = _lodash2.default.uniq(items.concat(_lodash2.default.get(action, 'response.result')));

      if (items.length >= total) {
        hasMore = false;
      } else {
        hasMore = true;
      }

      return _lodash2.default.merge({}, state, _defineProperty({}, id, {
        isFetching: false,
        hasMore: hasMore,
        error: null,
        items: items,
        total: total,
        lastUpdated: action.receivedAt
      }));
    default:
      return state;
  }
}

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

  switch (action.type) {
    case types.FETCH_RELATED_ARTICLES_REQUEST:
    case types.FETCH_RELATED_ARTICLES_FAILURE:
    case types.FETCH_RELATED_ARTICLES_SUCCESS:
      return articles(state, action);
    default:
      return state;
  }
}

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

  switch (action.type) {
    case types.FETCH_ARTICLES_BY_GROUP_UUID_REQUEST:
    case types.FETCH_ARTICLES_BY_GROUP_UUID_FAILURE:
    case types.FETCH_ARTICLES_BY_GROUP_UUID_SUCCESS:
    case types.FETCH_INDEX_ARTICLES_REQUEST:
    case types.FETCH_INDEX_ARTICLES_FAILURE:
    case types.FETCH_INDEX_ARTICLES_SUCCESS:
      return articles(state, action);
    default:
      return state;
  }
}

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

  switch (action.type) {
    case types.FETCH_SECTIONS_FEATURED_SUCCESS:
      var res = {};
      for (var section in action.response) {
        res[section] = action['response'][section]['result'];
      }
      return _lodash2.default.merge({}, state, {
        isFetching: false,
        fetched: true,
        error: null,
        items: res,
        lastUpdated: action.receivedAt
      });
    case types.FETCH_SECTION_FEATURED_FAILURE:
      return _lodash2.default.merge({}, state, {
        isFetching: false,
        fetched: false,
        error: action.error,
        lastUpdated: action.failedAt
      });
    default:
      return state;
  }
}

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

  switch (action.type) {
    case types.FETCH_CHOICES_SUCCESS:
      return {
        isFetching: false,
        fetched: true,
        error: null,
        items: _lodash2.default.get(action, 'response.result'),
        lastUpdated: action.receivedAt
      };
    case types.FETCH_CHOICES_FAILURE:
      return _lodash2.default.merge({}, state, {
        isFetching: false,
        fetched: false,
        error: action.error,
        lastUpdated: action.failedAt
      });
    default:
      return state;
  }
}

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

  switch (action.type) {
    case types.FETCH_LATEST_POSTS_SUCCESS:
      var orig = _lodash2.default.values(state['items']);
      var new_array = _lodash2.default.values(_lodash2.default.get(action, 'response.result'));
      return _lodash2.default.merge({}, state, {
        isFetching: false,
        fetched: true,
        error: null,
        items: new_array.concat(orig),
        meta: action.meta,
        links: action.links,
        lastUpdated: action.receivedAt
      });
    case types.FETCH_LATEST_POSTS_FAILURE:
      return _lodash2.default.merge({}, state, {
        isFetching: false,
        fetched: false,
        error: action.error,
        lastUpdated: action.failedAt
      });
    default:
      return state;
  }
}

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

  switch (action.type) {
    case types.FETCH_EVENT_SUCCESS:
      return _lodash2.default.merge({}, state, {
        isFetching: false,
        fetched: true,
        error: null,
        items: action.response.length > 0 ? action.response : {},
        meta: action.meta,
        links: action.links,
        lastUpdated: action.receivedAt
      });
    case types.FETCH_EVENT_FAILURE:
      return _lodash2.default.merge({}, state, {
        isFetching: false,
        fetched: false,
        error: action.error,
        lastUpdated: action.failedAt
      });
    default:
      return state;
  }
}

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

  switch (action.type) {
    case types.FETCH_TOPICS_SUCCESS:
      var res = {};
      for (var _topic in action.response) {
        res[action['response'][_topic]['id']] = action['response'][_topic];
      }
      return _lodash2.default.merge({}, state, {
        isFetching: false,
        fetched: true,
        error: null,
        items: res,
        meta: action.meta,
        links: action.links,
        lastUpdated: action.receivedAt
      });
    case types.FETCH_TOPICS_FAILURE:
      return _lodash2.default.merge({}, state, {
        isFetching: false,
        fetched: false,
        error: action.error,
        lastUpdated: action.failedAt
      });
    default:
      return state;
  }
}

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

  switch (action.type) {
    case types.FETCH_TOPIC_SUCCESS:
      return _lodash2.default.merge({}, state, {
        isFetching: false,
        fetched: true,
        error: null,
        items: action.response,
        lastUpdated: action.receivedAt
      });
    case types.FETCH_TOPIC_FAILURE:
      return _lodash2.default.merge({}, state, {
        isFetching: false,
        fetched: false,
        error: action.error,
        lastUpdated: action.failedAt
      });
    default:
      return state;
  }
}

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

  switch (action.type) {
    case types.FETCH_IMAGES_SUCCESS:
      return _lodash2.default.merge({}, state, {
        isFetching: false,
        fetched: true,
        error: null,
        items: action.response,
        lastUpdated: action.receivedAt
      });
    case types.FETCH_IMAGES_FAILURE:
      return _lodash2.default.merge({}, state, {
        isFetching: false,
        fetched: false,
        error: action.error,
        lastUpdated: action.failedAt
      });
    default:
      return state;
  }
}

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

  switch (action.type) {
    case types.FETCH_AUDIOS_SUCCESS:
      var orig = _lodash2.default.values(state['items']);
      var new_array = _lodash2.default.values(_lodash2.default.get(action, 'response'));
      return _lodash2.default.merge({}, state, {
        isFetching: false,
        fetched: true,
        error: null,
        items: orig.concat(new_array),
        meta: action.meta,
        links: action.links,
        lastUpdated: action.receivedAt
      });
    case types.FETCH_AUDIOS_FAILURE:
      return _lodash2.default.merge({}, state, {
        isFetching: false,
        fetched: false,
        error: action.error,
        lastUpdated: action.failedAt
      });
    default:
      return state;
  }
}

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

  switch (action.type) {
    case types.FETCH_SECTION_LIST_REQUEST:
      return _lodash2.default.merge({}, state, {
        url: action.url,
        lastUpdated: action.requestAt
      });
    case types.FETCH_SECTION_LIST_SUCCESS:
      return _lodash2.default.merge({}, state, {
        error: null,
        fetched: true,
        response: action.response,
        lastUpdated: action.receivedAt
      });
    case types.FETCH_SECTION_LIST_FAILURE:
      return _lodash2.default.merge({}, state, {
        error: action.error,
        lastUpdated: action.failedAt
      });
    default:
      return state;
  }
}

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

  switch (action.type) {
    case types.FETCH_INDEX_ARTICLES_REQUEST:
      return _lodash2.default.merge({}, state, {
        isFetching: true,
        error: null
      });
    case types.FETCH_INDEX_ARTICLES_FAILURE:
      return _lodash2.default.merge({}, state, {
        isFetching: false,
        error: action.error,
        lastUpdated: action.failedAt
      });
    default:
      return state;
  }
}

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

  switch (action.type) {
    case types.FETCH_SEARCH_RESULT_REQUEST:
      return _lodash2.default.merge({}, state, {
        isFetching: true,
        url: action.url,
        requestAt: action.requestAt
      });
    case types.FETCH_SEARCH_RESULT_SUCCESS:
      return _lodash2.default.merge({}, state, {
        isFetching: false,
        response: action.response,
        receivedAt: action.receivedAt
      });
    case types.FETCH_SEARCH_RESULT_FAILURE:
      return _lodash2.default.merge({}, state, {
        isFetching: false,
        error: action.error,
        failedAt: action.failedAt
      });
    default:
      return state;
  }
}

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

  switch (action.type) {
    case types.FETCH_YOUTUBE_PLAYLIST_REQUEST:
      return _lodash2.default.merge({}, state, {
        isFetching: true,
        url: action.url,
        requestAt: action.requestAt
      });
    case types.FETCH_YOUTUBE_PLAYLIST_SUCCESS:
      var orig = _lodash2.default.values(state['items']);
      var new_array = _lodash2.default.values(_lodash2.default.get(action, ['response', 'items'], []));
      return _lodash2.default.merge({}, state, {
        isFetching: false,
        items: orig.concat(new_array),
        pageInfo: _lodash2.default.get(action, ['response', 'pageInfo'], {}),
        nextPageToken: _lodash2.default.get(action, ['response', 'nextPageToken'], ''),
        receivedAt: action.receivedAt
      });
    case types.FETCH_YOUTUBE_PLAYLIST_FAILURE:
      return _lodash2.default.merge({}, state, {
        isFetching: false,
        error: action.error,
        failedAt: action.failedAt
      });
    default:
      return state;
  }
}

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

  switch (action.type) {
    case types.FETCH_TWITTER_TIMELINE_REQUEST:
      return _lodash2.default.merge({}, state, {
        isFetching: true,
        url: action.url,
        requestAt: action.requestAt
      });
    case types.FETCH_TWITTER_TIMELINE_SUCCESS:
      var orig = _lodash2.default.values(state['items']);
      var new_array = _lodash2.default.values(action);
      return _lodash2.default.merge({}, state, {
        isFetching: false,
        items: orig.concat(new_array),
        receivedAt: action.receivedAt
      });
    case types.FETCH_TWITTER_TIMELINE_FAILURE:
      return _lodash2.default.merge({}, state, {
        isFetching: false,
        error: action.error,
        failedAt: action.failedAt
      });
    default:
      return state;
  }
}

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

  switch (action.type) {
    case types.FETCH_FEATURE_ARTICLES_REQUEST:
      return _lodash2.default.merge({}, state, {
        isFetching: true,
        error: null
      });
    case types.FETCH_FEATURE_ARTICLES_FAILURE:
      return _lodash2.default.merge({}, state, {
        isFetching: false,
        error: action.error,
        lastUpdated: action.failedAt
      });
    case types.FETCH_FEATURE_ARTICLES_SUCCESS:
      return {
        isFetching: false,
        error: null,
        items: _lodash2.default.get(action, 'response.result'),
        lastUpdated: action.receivedAt
      };
    default:
      return state;
  }
}