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

function modifyProtectedBranchRule(_ref) {
  var projectName = _ref.projectName,
    privateToken = _ref.privateToken,
    _ref$baseUrl = _ref.baseUrl,
    baseUrl = _ref$baseUrl === void 0 ? T_GIT_BASE_URL : _ref$baseUrl,
    ruleId = _ref.ruleId,
    form = _ref.form;
  return new Promise(function (resolve, reject) {
    if (!projectName) {
      reject('No ProjectName');
      return;
    }
    if (!ruleId) {
      reject('No RuleId');
      return;
    }
    instance({
      url: resolveBaseUrl(baseUrl, "/api/v3/projects/".concat(encodeURIComponent(projectName), "/protected_branch_rules/").concat(ruleId)),
      method: 'PUT',
      data: Object.assign({}, form),
      headers: {
        'PRIVATE-TOKEN': privateToken
      }
    }).then(function (res) {
      resolve(res.data);
    })["catch"](function (err) {
      reject(err);
    });
  });
}
function createProtectedBranchRule(_ref2) {
  var projectName = _ref2.projectName,
    privateToken = _ref2.privateToken,
    _ref2$baseUrl = _ref2.baseUrl,
    baseUrl = _ref2$baseUrl === void 0 ? T_GIT_BASE_URL : _ref2$baseUrl,
    form = _ref2.form;
  return new Promise(function (resolve, reject) {
    if (!projectName) {
      reject('No ProjectName');
      return;
    }
    instance({
      url: resolveBaseUrl(baseUrl, "/api/v3/projects/".concat(encodeURIComponent(projectName), "/protected_branch_rules")),
      method: 'POST',
      data: Object.assign({}, form),
      headers: {
        'PRIVATE-TOKEN': privateToken
      }
    }).then(function (res) {
      resolve(res.data);
    })["catch"](function (err) {
      reject(err);
    });
  });
}
// 获取项目的自动保护分支规则
function getAutoProtectedBranchRules(_ref3) {
  var projectName = _ref3.projectName,
    _ref3$baseUrl = _ref3.baseUrl,
    baseUrl = _ref3$baseUrl === void 0 ? T_GIT_BASE_URL : _ref3$baseUrl,
    privateToken = _ref3.privateToken;
  return new Promise(function (resolve, reject) {
    if (!projectName) {
      reject('No ProjectName');
      return;
    }
    instance({
      url: resolveBaseUrl(baseUrl, "/api/v3/projects/".concat(encodeURIComponent(projectName), "/config/auto_protected_branch_rules")),
      method: 'GET',
      headers: {
        'PRIVATE-TOKEN': privateToken
      }
    }).then(function (res) {
      resolve(res.data);
    })["catch"](function (err) {
      reject(err);
    });
  });
}
// 修改项目的自动保护分支规则。可以通过正则表达式，让分支在创建之后自动匹配到对应的保护分支规则组
// 语法为 `规则组名` REGEXP "正则表达式" 。多个用换行符分隔。
// `branch_rule_1` REGEXP "branch_01.*"
// `branch_rule_dev` REGEXP "dev.*"
function modifyAutoProtectedBranchRules(_ref4) {
  var projectName = _ref4.projectName,
    _ref4$baseUrl = _ref4.baseUrl,
    baseUrl = _ref4$baseUrl === void 0 ? T_GIT_BASE_URL : _ref4$baseUrl,
    privateToken = _ref4.privateToken,
    rules = _ref4.rules,
    _ref4$shouldUpdateExi = _ref4.shouldUpdateExistingBranches,
    shouldUpdateExistingBranches = _ref4$shouldUpdateExi === void 0 ? true : _ref4$shouldUpdateExi;
  return new Promise(function (resolve, reject) {
    if (!projectName) {
      reject('No ProjectName');
      return;
    }
    instance({
      url: resolveBaseUrl(baseUrl, "/api/v3/projects/".concat(encodeURIComponent(projectName), "/config/auto_protected_branch_rules")),
      method: 'PUT',
      headers: {
        'PRIVATE-TOKEN': privateToken
      },
      data: {
        auto_protected_branch_rules: rules,
        should_update_existing_branches: shouldUpdateExistingBranches
      }
    }).then(function (res) {
      resolve(res.data);
    })["catch"](function (err) {
      reject(err);
    });
  });
}
// 返回项目下的规则组列表，按规则组的更新时间倒序
function getProtectedBranchRules(_ref5) {
  var projectName = _ref5.projectName,
    _ref5$baseUrl = _ref5.baseUrl,
    baseUrl = _ref5$baseUrl === void 0 ? T_GIT_BASE_URL : _ref5$baseUrl,
    privateToken = _ref5.privateToken;
  return new Promise(function (resolve, reject) {
    if (!projectName) {
      reject('No ProjectName');
      return;
    }
    instance({
      url: resolveBaseUrl(baseUrl, "/api/v3/projects/".concat(encodeURIComponent(projectName), "/protected_branch_rules")),
      method: 'GET',
      headers: {
        'PRIVATE-TOKEN': privateToken
      }
    }).then(function (res) {
      resolve(res.data);
    })["catch"](function (err) {
      reject(err);
    });
  });
}

export { createProtectedBranchRule, getAutoProtectedBranchRules, getProtectedBranchRules, modifyAutoProtectedBranchRules, modifyProtectedBranchRule };
