{"size": 707, "ext": "js", "max_stars_repo_path": "shoutem.cms/server/src/cms-dashboard/services/resource.js", "max_stars_repo_name": "StavonJoy/extensions", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "shoutem.cms/server/src/cms-dashboard/services/resource.js", "max_issues_repo_name": "StavonJoy/extensions", "max_issues_repo_issues_event_min_datetime": "2021-01-11T17:38:51.000Z", "max_issues_repo_issues_event_max_datetime": "2021-01-11T17:38:51.000Z", "max_forks_repo_path": "shoutem.cms/server/src/cms-dashboard/services/resource.js", "max_forks_repo_name": "StavonJoy/extensions", "max_forks_repo_forks_event_min_datetime": "2020-12-18T22:14:50.000Z", "max_forks_repo_forks_event_max_datetime": "2021-01-07T17:50:46.000Z"}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": 5, "avg_line_length": 20.2, "max_line_length": 61, "alphanum_fraction": 0.6209335219}
import _ from 'lodash';

function isExternalReference(element) {
  if (_.isArray(element)) {
    return isExternalReference(_.head(element));
  }

  return _.has(element, 'id') && _.has(element, 'type');
}

function transformToExternalReference(element) {
  if (_.isArray(element)) {
    return _.map(element, transformToExternalReference);
  }

  return _.pick(element, ['id', 'type']);
}

export function getResourceRelationships(resource) {
  return _.reduce(
    resource,
    (result, value, key) => {
      if (!isExternalReference(value)) {
        return result;
      }

      return {
        ...result,
        [key]: { data: transformToExternalReference(value) },
      };
    },
    {},
  );
}
