'use strict';

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

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

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

function getArticleFieldToEntity() {
  return [{
    field: 'designers',
    entity: 'authors'
  }, {
    field: 'engineers',
    entity: 'authors'
  }, {
    field: 'photographers',
    entity: 'authors'
  }, {
    field: 'writters',
    entity: 'authors'
  }, {
    field: 'relateds',
    entity: 'articles'
  }, {
    field: 'tags',
    entity: 'tags'
  }, {
    field: 'categories',
    entity: 'categories'
  }, {
    field: 'topics',
    entity: 'topics'
  }];
}

function denormalizeEntity() {
  var entityIds = arguments.length <= 0 || arguments[0] === undefined ? [] : arguments[0];
  var entityObj = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];

  var rtn = void 0;
  entityObj = entityObj || {};
  if (Array.isArray(entityIds)) {
    rtn = [];
    _lodash2.default.forEach(entityIds, function (id) {
      if (entityObj.hasOwnProperty(id)) {
        rtn.push(entityObj[id]);
      }
    });
  } else {
    rtn = entityObj[entityIds];
  }
  return rtn;
}

function denormalizeArticles() {
  var ids = arguments.length <= 0 || arguments[0] === undefined ? [] : arguments[0];
  var entities = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];

  var denormalizedArticles = [];
  // extract entities articles need
  var articles = entities.articles;

  var fieldToEntity = getArticleFieldToEntity();

  ids = Array.isArray(ids) ? ids : [ids];
  ids.forEach(function (id) {
    if (articles.hasOwnProperty(id)) {
      (function () {
        var article = _lodash2.default.merge({}, articles[id]);
        _lodash2.default.forEach(fieldToEntity, function (ele) {
          article[ele.field] = denormalizeEntity(article[ele.field], entities[ele.entity]);
        });
        denormalizedArticles.push(article);
      })();
    }
  });
  return denormalizedArticles;
}

function shallowDenormalizeArticles() {
  var ids = arguments.length <= 0 || arguments[0] === undefined ? [] : arguments[0];
  var entities = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];

  var denormalizedArticles = [];
  // extract entities articles need
  var articles = entities.articles;


  ids = Array.isArray(ids) ? ids : [ids];
  ids.forEach(function (id) {
    if (articles.hasOwnProperty(id)) {
      var article = _lodash2.default.merge({}, articles[id]);
      denormalizedArticles.push(article);
    }
  });
  return denormalizedArticles;
}