import _regeneratorRuntime from '@babel/runtime/regenerator';
import { _ as __awaiter } from '../tslib.es6-848120af.js';
import axios from 'axios';
import { getDevopsAccessToken } from './token.mjs';

function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function getPipelineReviewElement() {
  var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var _a, _b;
  var stages = ((_a = data === null || data === void 0 ? void 0 : data.model) === null || _a === void 0 ? void 0 : _a.stages) || [];
  var _iterator = _createForOfIteratorHelper(stages),
    _step;
  try {
    for (_iterator.s(); !(_step = _iterator.n()).done;) {
      var stage = _step.value;
      var containers = (stage === null || stage === void 0 ? void 0 : stage.containers) || [];
      var _iterator2 = _createForOfIteratorHelper(containers),
        _step2;
      try {
        for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
          var container = _step2.value;
          var elements = (container === null || container === void 0 ? void 0 : container.elements) || [];
          var _iterator3 = _createForOfIteratorHelper(elements),
            _step3;
          try {
            for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
              var element = _step3.value;
              if (((_b = element === null || element === void 0 ? void 0 : element.reviewUsers) === null || _b === void 0 ? void 0 : _b.length) && (element === null || element === void 0 ? void 0 : element.status) === 'REVIEWING') {
                return element;
              }
            }
          } catch (err) {
            _iterator3.e(err);
          } finally {
            _iterator3.f();
          }
        }
      } catch (err) {
        _iterator2.e(err);
      } finally {
        _iterator2.f();
      }
    }
  } catch (err) {
    _iterator.e(err);
  } finally {
    _iterator.f();
  }
  return [];
}
function getReviewingReviewers() {
  var data = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  var element = getPipelineReviewElement(data);
  if (Array.isArray(element)) {
    return [];
  }
  return element.actualReviewUsers || element.reviewUsers;
}
function getPipelineBuildDetail(_ref) {
  var projectId = _ref.projectId,
    pipelineId = _ref.pipelineId,
    buildId = _ref.buildId,
    _ref$archiveFlag = _ref.archiveFlag,
    archiveFlag = _ref$archiveFlag === void 0 ? false : _ref$archiveFlag,
    _ref$executeCount = _ref.executeCount,
    executeCount = _ref$executeCount === void 0 ? '' : _ref$executeCount,
    secretInfo = _ref.secretInfo,
    host = _ref.host;
  var _a;
  return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    var appCode, appSecret, devopsUid, accessToken, resp;
    return _regeneratorRuntime.wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          appCode = secretInfo.appCode, appSecret = secretInfo.appSecret, devopsUid = secretInfo.devopsUid;
          _context.next = 1;
          return getDevopsAccessToken({
            secretInfo: secretInfo,
            host: host
          });
        case 1:
          accessToken = _context.sent;
          _context.next = 2;
          return axios({
            url: "".concat(host, "/prod/v4/apigw-app/projects/").concat(projectId, "/build_detail?buildId=").concat(buildId, "&pipelineId=").concat(pipelineId, "&archiveFlag=").concat(archiveFlag, "&executeCount=").concat(executeCount),
            method: 'GET',
            headers: {
              'X-DEVOPS-UID': devopsUid,
              'Content-Type': 'application/json',
              'X-Bkapi-Authorization': JSON.stringify({
                bk_app_code: appCode,
                bk_app_secret: appSecret,
                access_token: accessToken
              })
            }
          })["catch"](function (err) {
            console.log('[getPipelineBuildDetail] err: ', err);
          });
        case 2:
          resp = _context.sent;
          return _context.abrupt("return", ((_a = resp.data) === null || _a === void 0 ? void 0 : _a.data) || {});
        case 3:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
}

export { getPipelineBuildDetail, getPipelineReviewElement, getReviewingReviewers };
