import _regeneratorRuntime from '@babel/runtime/regenerator';
import { _ as __awaiter } from '../tslib.es6-848120af.js';
import { modifyAutoProtectedBranchRules, createProtectedBranchRule, getProtectedBranchRules, getAutoProtectedBranchRules } from './branch-protected.mjs';
import './helper.mjs';
import 'axios';

function genProjectRuleForm(ruleName) {
  return {
    name: ruleName
  };
}
function checkShouldCreateAutoProjectRule(rules) {
  var keyWord = 'REGEXP "feature/story-.*"';
  return !rules.includes(keyWord);
}
function getAutoProjectedRuleStr(ruleName) {
  return "`".concat(ruleName, "` REGEXP \"feature/story-.*\"");
}
function getTargetRule(rules) {
  if (!rules.length) return null;
  return rules.find(function (item) {
    return item.name.includes('develop');
  }) || rules[0];
}
function createAutoProtectedRuleForStoryBranch(_ref) {
  var projectName = _ref.projectName,
    baseUrl = _ref.baseUrl,
    privateToken = _ref.privateToken,
    _ref$defaultRuleName = _ref.defaultRuleName,
    defaultRuleName = _ref$defaultRuleName === void 0 ? 'rule_created_by_novlan1' : _ref$defaultRuleName,
    shouldUpdateExistingBranches = _ref.shouldUpdateExistingBranches;
  var _a;
  return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    var rule, shouldCreateAutoProjectRules, rulesList, ruleName, ruleStr;
    return _regeneratorRuntime.wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          _context.next = 1;
          return getAutoProtectedBranchRules({
            projectName: projectName,
            privateToken: privateToken,
            baseUrl: baseUrl
          });
        case 1:
          rule = _context.sent;
          console.log('[getProtectedBranchRules] res: ', rule);
          shouldCreateAutoProjectRules = checkShouldCreateAutoProjectRule(rule);
          if (shouldCreateAutoProjectRules) {
            _context.next = 2;
            break;
          }
          return _context.abrupt("return");
        case 2:
          _context.next = 3;
          return getProtectedBranchRules({
            projectName: projectName,
            privateToken: privateToken,
            baseUrl: baseUrl
          });
        case 3:
          rulesList = _context.sent;
          console.log('[getProtectedBranchRules] rulesList: ', rulesList);
          ruleName = defaultRuleName;
          if (rulesList.length) {
            _context.next = 5;
            break;
          }
          _context.next = 4;
          return createProtectedBranchRule({
            projectName: projectName,
            privateToken: privateToken,
            baseUrl: baseUrl,
            form: genProjectRuleForm(ruleName)
          });
        case 4:
          _context.next = 6;
          break;
        case 5:
          ruleName = ((_a = getTargetRule(rulesList)) === null || _a === void 0 ? void 0 : _a.name) || '';
        case 6:
          if (ruleName) {
            _context.next = 7;
            break;
          }
          throw new Error('No ruleName');
        case 7:
          ruleStr = getAutoProjectedRuleStr(ruleName);
          console.log('[ruleStr]', ruleStr);
          _context.next = 8;
          return modifyAutoProtectedBranchRules({
            projectName: projectName,
            privateToken: privateToken,
            baseUrl: baseUrl,
            rules: ruleStr,
            shouldUpdateExistingBranches: shouldUpdateExistingBranches
          });
        case 8:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
}

export { createAutoProtectedRuleForStoryBranch };
