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

exports.tags = tags;
exports.categories = categories;

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 groups() {
  var state = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
  var action = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];

  var _groups = {};

  var _ret = function () {
    switch (action.type) {
      case types.FETCH_TAGS_REQUEST:
      case types.FETCH_CATEGORIES_REQUEST:
        action.groups.forEach(function (group) {
          _groups[group] = {
            error: null,
            isFetching: true
          };
        });
        return {
          v: Object.assign({}, state, _groups)
        };
      case types.FETCH_TAGS_FAILURE:
      case types.FETCH_CATEGORIES_FAILURE:
        action.groups.forEach(function (group) {
          _groups[group] = {
            error: action.error,
            isFetching: false,
            lastUpdated: action.failedAt
          };
        });
        return {
          v: Object.assign({}, state, _groups)
        };
      case types.FETCH_TAGS_SUCCESS:
      case types.FETCH_CATEGORIES_SUCCESS:
        var entities = void 0;
        if (action.type === types.FETCH_TAGS_SUCCESS) {
          entities = _lodash2.default.get(action, ['response', 'entities', 'tags'], []);
        } else {
          entities = _lodash2.default.get(action, ['response', 'entities', 'categories'], []);
        }

        Object.keys(entities).forEach(function (entityId) {
          _groups[entities[entityId].name] = {
            id: entityId,
            error: null,
            isFetching: false,
            lastUpdated: action.receivedAt
          };
        });
        return {
          v: Object.assign({}, state, _groups)
        };
      default:
        return {
          v: state
        };
    }
  }();

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

function tags() {
  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_TAGS_REQUEST:
    case types.FETCH_TAGS_FAILURE:
    case types.FETCH_TAGS_SUCCESS:
      return groups(state, action);
    default:
      return state;
  }
}

function categories() {
  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_CATEGORIES_REQUEST:
    case types.FETCH_CATEGORIES_FAILURE:
    case types.FETCH_CATEGORIES_SUCCESS:
      return groups(state, action);
    default:
      return state;
  }
}