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

/**
 * 启动流水线
 * @param {object} params 配置信息
 * @param {string} params.projectId 项目ID
 * @param {string} params.pipelineId 流水线ID
 * @param {object} params.secretInfo 密钥信息
 * @param {string} params.host 请求域名
 * @param {object} params.data 请求数据
 * @example
 * ```ts
 * await startDevopsPipeline({
 *   projectId: 'my-project',
 *   pipelineId: 'p-xxxx',
 *   host: 'https://devops.woa.com',
 *   secretInfo: {
 *     appCode: 'xxx',
 *     appSecret: 'xxx',
 *     devopsUid: 'novlan1',
 *   },
 *   data: { buildNo: 1 },
 * });
 * ```
 */
function startDevopsPipeline(_ref) {
  var projectId = _ref.projectId,
    pipelineId = _ref.pipelineId,
    secretInfo = _ref.secretInfo,
    host = _ref.host,
    data = _ref.data;
  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_start?pipelineId=").concat(pipelineId),
            method: 'post',
            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
              })
            },
            data: Object.assign({}, data || {})
          })["catch"](function (err) {
            console.log('[startDevopsPipeline] err: ', err);
          });
        case 2:
          resp = _context.sent;
          return _context.abrupt("return", resp.data);
        case 3:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
}
/**
 * 获取流水线列表
 * @param {object} params 配置信息
 * @param {string} params.projectId 项目ID
 * @param {object} params.secretInfo 密钥信息
 * @param {string} params.host 请求域名
 * @param {number} params.page 第几页
 * @param {number} params.pageSize 每页数据量
 * @returns 流水线列表
 * @example
 * ```ts
 * const res = await getPipelineList({
 *   projectId: 'my-project',
 *   host: 'https://devops.woa.com',
 *   secretInfo: { appCode, appSecret, devopsUid },
 *   page: 1,
 *   pageSize: 20,
 * });
 * console.log(res.records);
 * ```
 */
function getPipelineList(_ref2) {
  var projectId = _ref2.projectId,
    secretInfo = _ref2.secretInfo,
    host = _ref2.host,
    _ref2$page = _ref2.page,
    page = _ref2$page === void 0 ? 1 : _ref2$page,
    _ref2$pageSize = _ref2.pageSize,
    pageSize = _ref2$pageSize === void 0 ? 20 : _ref2$pageSize;
  var _a;
  return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
    var appCode, appSecret, devopsUid, accessToken, resp;
    return _regeneratorRuntime.wrap(function (_context2) {
      while (1) switch (_context2.prev = _context2.next) {
        case 0:
          appCode = secretInfo.appCode, appSecret = secretInfo.appSecret, devopsUid = secretInfo.devopsUid;
          _context2.next = 1;
          return getDevopsAccessToken({
            secretInfo: secretInfo,
            host: host
          });
        case 1:
          accessToken = _context2.sent;
          _context2.next = 2;
          return axios({
            url: "".concat(host, "/prod/v4/apigw-app/projects/").concat(projectId, "/pipelines/pipeline_list?page=").concat(page, "&pageSize=").concat(pageSize),
            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('[getPipelineList] err: ', err);
          });
        case 2:
          resp = _context2.sent;
          return _context2.abrupt("return", ((_a = resp.data) === null || _a === void 0 ? void 0 : _a.data) || {});
        case 3:
        case "end":
          return _context2.stop();
      }
    }, _callee2);
  }));
}
/**
 * 获取全部流水线列表
 * @param {object} params 配置信息
 * @param {string} params.projectId 项目ID
 * @param {object} params.secretInfo 密钥信息
 * @param {string} params.host 请求域名
 * @param {number} params.page 第几页
 * @param {number} params.pageSize 每页数据量
 * @param {Array} list 结果列表，可不传，用于迭代
 * @returns 流水线列表
 * @example
 * ```ts
 * const all = await getAllPipelineList({
 *   projectId: 'my-project',
 *   host: 'https://devops.woa.com',
 *   secretInfo: { appCode, appSecret, devopsUid },
 * });
 * console.log(all.length);
 * ```
 */
function getAllPipelineList(args) {
  var list = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
    var page, res;
    return _regeneratorRuntime.wrap(function (_context3) {
      while (1) switch (_context3.prev = _context3.next) {
        case 0:
          page = args.page || 1;
          _context3.next = 1;
          return getPipelineList(Object.assign(Object.assign({}, args), {
            page: page
          }));
        case 1:
          res = _context3.sent;
          list.push.apply(list, _toConsumableArray(res.records || []));
          if (!(!res.count || list.length >= res.count)) {
            _context3.next = 2;
            break;
          }
          return _context3.abrupt("return", list);
        case 2:
          _context3.next = 3;
          return getAllPipelineList(Object.assign(Object.assign({}, args), {
            page: page + 1
          }), list);
        case 3:
          return _context3.abrupt("return", _context3.sent);
        case 4:
        case "end":
          return _context3.stop();
      }
    }, _callee3);
  }));
}

export { getAllPipelineList, getPipelineList, startDevopsPipeline };
