{"size": 795, "ext": "js", "max_stars_repo_path": "script/validations/utilities/get-model-category.js", "max_stars_repo_name": "flowcommerce/lib-apidoc", "max_stars_repo_stars_event_min_datetime": "2019-03-06T01:22:28.000Z", "max_stars_repo_stars_event_max_datetime": "2021-03-23T02:53:56.000Z", "max_issues_repo_path": "script/validations/utilities/get-model-category.js", "max_issues_repo_name": "flowcommerce/lib-apidoc", "max_issues_repo_issues_event_min_datetime": "2017-06-22T22:02:07.000Z", "max_issues_repo_issues_event_max_datetime": "2021-03-23T20:16:53.000Z", "max_forks_repo_path": "script/validations/utilities/get-model-category.js", "max_forks_repo_name": "flowcommerce/lib-apidoc", "max_forks_repo_forks_event_min_datetime": "2019-03-06T01:22:38.000Z", "max_forks_repo_forks_event_max_datetime": "2019-03-06T01:22:38.000Z"}
{"max_stars_count": 5, "max_issues_count": 4, "max_forks_count": 1, "avg_line_length": 24.84375, "max_line_length": 82, "alphanum_fraction": 0.6540880503}
import find from 'lodash/fp/find';

import getBaseType from './get-base-type';

/**
 * Determine the 'category' of a specific type from apidoc. The three valid
 * categories are:
 * - enum
 * - model
 * - union
 *
 * @param  {string} type    The apidoc type value from model.fields or union.types
 * @param  {Object} apiData The full api definition from apidoc
 */
const getModelCategory = (type, apiData) => {
  if (find(m => m.name === getBaseType(type), apiData.models)) {
    return 'model';
  }

  if (find(e => e.name === getBaseType(type), apiData.enums)) {
    return 'enum';
  }

  if (find(u => u.name === getBaseType(type), apiData.unions)) {
    return 'union';
  }

  throw new Error(`Could not determine api model category for type[${type}]`);
};

export default getModelCategory;
