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

/**
 * 创建 Webhook
 *
 * 对应工蜂 API：POST /api/v3/projects/:id/hooks
 *
 * @param {object} options 输入配置
 * @param {string | number} options.projectName 项目名称或项目 ID
 * @param {string} options.webhookUrl 回调 URL
 * @param {string} options.privateToken 密钥
 * @param {CreateWebhookOptions} [options.events] 事件配置，默认只开 merge_requests_events
 * @param {string} [options.baseUrl] baseUrl
 * @returns {Promise<WebhookItem>} Webhook 条目
 * @example
 *
 * createWebhook({
 *   projectName: 'group/sub/repo',
 *   webhookUrl: 'https://my-server.com/hooks/tgit',
 *   privateToken: 'xxxxx',
 *   events: {
 *     merge_requests_events: true,
 *     note_events: true,
 *   },
 * }).then((hook) => {
 *   console.log(hook.id);
 * })
 */
function createWebhook(_ref) {
  var projectName = _ref.projectName,
    webhookUrl = _ref.webhookUrl,
    privateToken = _ref.privateToken,
    events = _ref.events,
    baseUrl = _ref.baseUrl;
  return new Promise(function (resolve, reject) {
    if (!projectName || !webhookUrl) {
      reject(new Error('projectName, webhookUrl is required'));
      return;
    }
    var url = resolveBaseUrl(baseUrl, "/api/v3/projects/".concat(encodeProject(projectName), "/hooks"));
    var payload = Object.assign({
      url: webhookUrl,
      merge_requests_events: true,
      push_events: false,
      enable_ssl_verification: false
    }, events || {});
    instance({
      url: url,
      method: 'POST',
      data: payload,
      headers: {
        'PRIVATE-TOKEN': privateToken
      }
    }).then(function (res) {
      resolve(res.data);
    })["catch"](function (err) {
      reject(err);
    });
  });
}
/**
 * 获取项目的 Webhook 列表
 *
 * 对应工蜂 API：GET /api/v3/projects/:id/hooks
 * @example
 *
 * getHooks({
 *   projectName: 'group/sub/repo',
 *   privateToken: 'xxxxx',
 * }).then((list) => {
 *   list.forEach(h => console.log(h.id, h.url));
 * })
 */
function getHooks(_ref2) {
  var projectName = _ref2.projectName,
    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), "/hooks"));
    instance({
      url: url,
      method: 'GET',
      headers: {
        'PRIVATE-TOKEN': privateToken
      }
    }).then(function (res) {
      resolve(res.data || []);
    })["catch"](function (err) {
      reject(err);
    });
  });
}
/**
 * 删除 Webhook
 *
 * 对应工蜂 API：DELETE /api/v3/projects/:id/hooks/:hookId
 * @example
 *
 * deleteWebhook({
 *   projectName: 'group/sub/repo',
 *   hookId: 12345,
 *   privateToken: 'xxxxx',
 * }).then((resp) => {
 *
 * })
 */
function deleteWebhook(_ref3) {
  var projectName = _ref3.projectName,
    hookId = _ref3.hookId,
    privateToken = _ref3.privateToken,
    baseUrl = _ref3.baseUrl;
  return new Promise(function (resolve, reject) {
    if (!projectName || !hookId) {
      reject(new Error('projectName, hookId is required'));
      return;
    }
    var url = resolveBaseUrl(baseUrl, "/api/v3/projects/".concat(encodeProject(projectName), "/hooks/").concat(hookId));
    instance({
      url: url,
      method: 'DELETE',
      headers: {
        'PRIVATE-TOKEN': privateToken
      }
    }).then(function (res) {
      resolve(res.data);
    })["catch"](function (err) {
      reject(err);
    });
  });
}

export { createWebhook, deleteWebhook, getHooks };
