'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.relatedArticles = relatedArticles;
exports.articlesByUuids = articlesByUuids;
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:
      return articles(state, action);
    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;
  }
}