{"size": 2934, "ext": "js", "max_stars_repo_path": "src/api.js", "max_stars_repo_name": "DeskproApps/youtrack", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/api.js", "max_issues_repo_name": "DeskproApps/youtrack", "max_issues_repo_issues_event_min_datetime": "2018-03-07T15:25:12.000Z", "max_issues_repo_issues_event_max_datetime": "2019-03-13T17:59:21.000Z", "max_forks_repo_path": "src/api.js", "max_forks_repo_name": "DeskproApps/youtrack", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 12, "max_forks_count": null, "avg_line_length": 27.4205607477, "max_line_length": 108, "alphanum_fraction": 0.5886162236}

import { get, put, del, getApiUrl, notEmpty } from './utils';

/**
 * Returns a promise which resolves with a list of issues
 *
 * @returns { Promise }
 */
export const fetchIssue = issue => {
  if (issue) {
    return get(`${getApiUrl()}/issue/${issue}`);
  }
  return null;
};


/**
 * Returns issues matching the given filter
 *
 * @param {String} filter
 * @returns { Promise }
 */
export const searchIssues = (filter) => {
  const encodedFilter = encodeURIComponent(filter);
  return get(`${getApiUrl()}/issue?filter=${encodedFilter}`).then(resp => resp.body.issue);
};

// /**
//  * Returns issues matching the given filter
//  *
//  * @param {String} filter
//  * @returns { Promise }
//  */
// export const searchIssues = (filter) => {
//   const encodedFilter = encodeURIComponent(filter);
//   return get(`${getApiUrl()}/issue?filter=${encodedFilter}`)
//     .then((resp) => {
//       return resp.body.issue.map((issue) => {
//         let description = '';
//         for (let i = 0; i < issue.field.length; i++) {
//           if (issue.field[i].name === 'description') {
//             description = issue.field[i].value;
//             if (description.length > 10) {
//               description = `${description.substr(0, 10)}...`;
//             }
//           }
//         }
//
//         return `${issue.id} - ${description}`;
//       });
//     });
// };

/**
 * Returns a promise which resolves with a list of issues
 *
 * @returns { Promise }
 */
export const fetchIssues = (project) => {
  return get(`${getApiUrl()}/issue/count?filter=project:${project}`).then(
    resp => {
      let after = '';
      if (resp.body.value > 10) {
        after = `?after=${resp.body.value - 10}`
      }
      return get(`${getApiUrl()}/issue/byproject/${project}${after}`).then(resp => resp.body.reverse());
    }
  );
};

/**
 * Returns a promise which resolves with a list of projects
 *
 * @returns { Promise }
 */
export const fetchProjects = () => {
  return get(`${getApiUrl()}/project/all?verbose=false`);
};

/**
 * Returns a promise which resolves with a success or failure in creating a Youtrack issue
 *
 * @returns { Promise }
 */
export const createIssue = (data = {}) => {
  const { project = '', issueId = '', summary = '', desc = '' } = data;

  if (notEmpty(issueId)) {
    return fetchIssue(issueId);
  }

  return notEmpty(project) && notEmpty(summary) && notEmpty(desc) ?
    put(encodeURI(`${getApiUrl()}/issue?project=${project}&summary=${summary}&description=${desc}`), data) :
    Promise.reject(JSON.stringify({ message: 'Missing parameters: project, summary, desc' }));
};

/**
 * Returns a promise which resolves with a success or failure in deleting a Youtrack issue
 *
 * @returns { Promise }
 */
export const deleteIssue = (issue = '') => {
  return notEmpty(issue) ?
    del(`${getApiUrl()}/issue/${issue}`) :
    Promise.reject(JSON.stringify({ message: 'Missing parameter: issue' }));
};
