import { resolveBaseUrl, instance, encodeProject } from './helper.mjs';
import 'axios';

/**
 * 获取commit详情
 * @param {object} options 输入配置
 * @param {string} options.projectName 项目名称
 * @param {string} options.commitId 提交hash
 * @param {string} options.privateToken 密钥
 * @param {string} [options.baseUrl] 自定义 API 基础路径
 * @returns {Promise<object>} 请求Promise
 * @example
 *
 * getOneCommitDetail({
 *   projectName: 't-comm',
 *   commitId: 'aaaa',
 *   privateToken: 'xxxxx',
 * }).then((resp) => {
 *
 * })
 */
function getOneCommitDetail(_ref) {
  var projectName = _ref.projectName,
    commitId = _ref.commitId,
    privateToken = _ref.privateToken,
    baseUrl = _ref.baseUrl;
  return new Promise(function (resolve) {
    var url = "/api/v3/projects/".concat(encodeURIComponent(projectName), "/repository/commits/").concat(encodeURIComponent(commitId));
    url = resolveBaseUrl(baseUrl, url);
    instance({
      url: url,
      method: 'GET',
      headers: {
        'PRIVATE-TOKEN': privateToken
      }
    }).then(function (res) {
      resolve(res.data);
    })["catch"](function () {
      resolve({});
    });
  });
}
/**
 * 获取分支上的最新若干条 commit
 *
 * 对应工蜂 API：GET /api/v3/projects/:id/repository/commits
 *
 * 典型场景：通过 PUT files 接口更新文件后，该接口不返回 commit_id，
 * 可用本函数回查真实的 commit sha。
 *
 * @param {object} options 输入配置
 * @param {string | number} options.projectName 项目名称或项目 ID
 * @param {string} [options.refName] 分支名或 commit sha，默认由后端使用默认分支
 * @param {number} [options.perPage=1] 每页条数
 * @param {number} [options.page] 页码
 * @param {string} options.privateToken 密钥
 * @param {string} [options.baseUrl] baseUrl
 * @returns {Promise<Array<{ id: string; short_id: string; title?: string; message?: string; created_at?: string }>>} commit 列表
 * @example
 *
 * // 拉取 master 分支最近 5 条 commit
 * getLatestCommits({
 *   projectName: 'group/sub/repo',
 *   refName: 'master',
 *   perPage: 5,
 *   privateToken: 'xxxxx',
 * }).then((list) => {
 *   console.log(list[0].id); // commit sha
 * })
 */
function getLatestCommits(_ref2) {
  var projectName = _ref2.projectName,
    refName = _ref2.refName,
    _ref2$perPage = _ref2.perPage,
    perPage = _ref2$perPage === void 0 ? 1 : _ref2$perPage,
    page = _ref2.page,
    privateToken = _ref2.privateToken,
    baseUrl = _ref2.baseUrl;
  return new Promise(function (resolve, reject) {
    if (!projectName) {
      reject(new Error('projectName is required'));
      return;
    }
    var url = resolveBaseUrl(baseUrl, "/api/v3/projects/".concat(encodeProject(projectName), "/repository/commits"));
    var params = {
      per_page: perPage
    };
    if (refName) params.ref_name = refName;
    if (page !== undefined) params.page = page;
    instance({
      url: url,
      method: 'GET',
      params: params,
      headers: {
        'PRIVATE-TOKEN': privateToken
      }
    }).then(function (res) {
      resolve(res.data || []);
    })["catch"](function (err) {
      reject(err);
    });
  });
}

export { getLatestCommits, getOneCommitDetail };
