{"size": 1412, "ext": "js", "max_stars_repo_path": "lib/utils/build-project-version-document.js", "max_stars_repo_name": "Addepar/ember-cli-addon-docs", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "lib/utils/build-project-version-document.js", "max_issues_repo_name": "Addepar/ember-cli-addon-docs", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lib/utils/build-project-version-document.js", "max_forks_repo_name": "Addepar/ember-cli-addon-docs", "max_forks_repo_forks_event_min_datetime": "2020-05-13T11:29:42.000Z", "max_forks_repo_forks_event_max_datetime": "2020-05-13T11:29:42.000Z"}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 26.1481481481, "max_line_length": 90, "alphanum_fraction": 0.6558073654}
'use strict';

const Inflected = require('inflected');

module.exports = function buildProjectVersionDocument(docs, projectName, projectVersion) {
  versionIds(docs, projectName, projectVersion);

  return {
    data: versionDocument(docs, projectName, projectVersion),
    included: docs
  };
};

function versionIds(docs, projectName, projectVersion) {
  for (let doc of docs) {
    doc.id = `${doc.id}-${projectVersion}`;
    doc.relationships['project-version'] = {
      data: { type: 'project-version', id: `${projectName}-${projectVersion}` }
    };

    for (let key of Object.keys(doc.relationships)) {
      let relationship = doc.relationships[key];
      if (Array.isArray(relationship)) {
        versionIds(relationship, projectName, projectVersion);
      } else if (relationship && relationship.id) {
        versionIds([relationship], projectName, projectVersion);
      }
    }
  }
}

function versionDocument(docs, projectName, projectVersion) {
  let relationships = {};

  for (let doc of docs) {
    let pluralType = Inflected.pluralize(doc.type);
    if (!relationships[pluralType]) { relationships[pluralType] = { data: [] }; }

    relationships[pluralType].data.push({
      id: doc.id,
      type: doc.type
    });
  }

  return {
    type: 'project-version',
    id: `${projectName}-${projectVersion}`,
    relationships,
    attributes: {
      version: projectVersion
    },
  };
}
