Source: entities/entry.js

'use strict';

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

var _freeze = require('babel-runtime/core-js/object/freeze');

var _freeze2 = _interopRequireDefault(_freeze);

exports.wrapEntry = wrapEntry;
exports.wrapEntryCollection = wrapEntryCollection;

var _lang = require('lodash/lang');

var _array = require('lodash/array');

var _toPlainObject = require('../mixins/to-plain-object');

var _toPlainObject2 = _interopRequireDefault(_toPlainObject);

var _linkGetters = require('../mixins/link-getters');

var _linkGetters2 = _interopRequireDefault(_linkGetters);

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

/**
 * @memberof Entities
 * @typedef Entry
 * @prop {Entities.Sys} sys - Standard system metadata with additional entry specific properties
 * @prop {Entities.Link} sys.contentType - Content Type used by this Entry
 * @prop {string=} sys.locale - If present, indicates the locale which this entry uses
 * @prop {Object<EntryFields.Field>} fields - Object with content for each field
 * @prop {function(): Object} toPlainObject() - Returns this Entry as a plain JS object
 */

/**
 * @private
 * @param {Object} data - Raw entry data
 * @return {Entry} Wrapped entry data
 */
function wrapEntry(data) {
  return (0, _freeze2.default)((0, _toPlainObject2.default)((0, _lang.cloneDeep)(data)));
}

/**
 * @memberof Entities
 * @typedef EntryCollection
 * @prop {number} total
 * @prop {number} skip
 * @prop {number} limit
 * @prop {Array<Entities.Entry>} items
 * @prop {function(): Object} toPlainObject() - Returns this Entry collection as a plain JS object
 */

/**
 * Data is also mixed in with link getters if links exist and includes were requested
 * @private
 * @param {Object} data - Raw entry collection data
 * @return {EntryCollection} Wrapped entry collection data
 */
function wrapEntryCollection(data, resolveLinks) {
  var wrappedData = (0, _toPlainObject2.default)((0, _lang.cloneDeep)(data));
  if (resolveLinks) {
    var includes = prepareIncludes(wrappedData.includes, wrappedData.items);
    (0, _linkGetters2.default)(wrappedData.items, includes);
  }
  return (0, _freeze2.default)(wrappedData);
}

function prepareIncludes() {
  var includes = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
  var items = arguments[1];

  includes.Entry = includes.Entry || [];
  includes.Entry = (0, _array.uniq)(includes.Entry.concat((0, _lang.cloneDeep)(items)));
  return includes;
}