import _regeneratorRuntime from '@babel/runtime/regenerator';
import { _ as __awaiter } from '../tslib.es6-848120af.js';
import { getVersion } from './helper/helper.mjs';
import { baseRequestRainbow } from './helper/rainbow-base-request.mjs';
import 'axios';
import './helper/rainbow-signature.mjs';
import '../nodejs/crypto.mjs';

/**
 * 添加或更新配置
 *
 * @param {object} config 配置信息
 * @param {object} config.keyValue 配置对象
 * @param {string} config.keyValue.key 配置的key
 * @param {string} config.keyValue.value 配置的value
 * @param {number} config.valueType 配置类型，1: NUMBER, 2: STRING, 3: TEXT, 4: JSON, 5: XML, 18: 日期, 20: yaml
 * @param {object} config.secretInfo 密钥信息
 * @param {string} config.secretInfo.appId 项目Id
 * @param {string} config.secretInfo.userId 用户Id
 * @param {string} config.secretInfo.secretKey 密钥
 * @param {string} config.secretInfo.envName 配置环境
 * @param {string} config.secretInfo.groupName 配置组
 * @returns {Promise<object>} 请求Promise
 *
 * @example
 * addOrUpdateRainbowKV({
 *   keyValue: {
 *     key: 'theKey',
 *     value: 'theValue',
 *   },
 *   valueType: 2,
 *   secretInfo: {
 *     appId: 'xxx',
 *     userId: 'xxx',
 *     secretKey: 'xxx',
 *     envName: 'prod',
 *     groupName: 'xxx',
 *   }
 * }).then(() => {
 *
 * })
 */
function addOrUpdateRainbowKV(_ref) {
  var keyValue = _ref.keyValue,
    _ref$valueType = _ref.valueType,
    valueType = _ref$valueType === void 0 ? 2 : _ref$valueType,
    secretInfo = _ref.secretInfo;
  return baseRequestRainbow({
    url: '/adminapi.Config/ChangeKeyReq',
    data: {
      is_sync: false,
      key_values: [Object.assign({
        value_type: valueType,
        config_op_type: 4,
        description: ''
      }, keyValue)]
    },
    secretInfo: secretInfo
  });
}
/**
 * 增加配置
 *
 * @param {object} config 配置信息
 * @param {object} config.keyValue 配置对象
 * @param {string} config.keyValue.key 配置的key
 * @param {string} config.keyValue.value 配置的value
 * @param {number} config.valueType 配置类型，1: NUMBER, 2: STRING, 3: TEXT, 4: JSON, 5: XML, 18: 日期, 20: yaml
 * @param {object} config.secretInfo 密钥信息
 * @param {string} config.secretInfo.appId 项目Id
 * @param {string} config.secretInfo.userId 用户Id
 * @param {string} config.secretInfo.secretKey 密钥
 * @param {string} config.secretInfo.envName 配置环境
 * @param {string} config.secretInfo.groupName 配置组
 * @returns {Promise<object>} 请求Promise
 *
 * @example
 * addRainbowKV({
 *   keyValue: {
 *     key: 'theKey',
 *     value: 'theValue',
 *   },
 *   valueType: 2,
 *   secretInfo: {
 *     appId: 'xxx',
 *     userId: 'xxx',
 *     secretKey: 'xxx',
 *     envName: 'prod',
 *     groupName: 'xxx',
 *   }
 * }).then(() => {
 *
 * })
 */
function addRainbowKV(_ref2) {
  var keyValue = _ref2.keyValue,
    _ref2$valueType = _ref2.valueType,
    valueType = _ref2$valueType === void 0 ? 2 : _ref2$valueType,
    secretInfo = _ref2.secretInfo;
  return baseRequestRainbow({
    url: '/adminapi.Config/ChangeKeyReq',
    data: {
      is_sync: false,
      key_values: [Object.assign({
        value_type: valueType,
        config_op_type: 2,
        description: ''
      }, keyValue)]
    },
    secretInfo: secretInfo
  });
}
/**
 * 修改配置
 * @param {object} config 配置信息
 * @param {object} config.keyValue 配置对象
 * @param {string} config.keyValue.key 配置的key
 * @param {string} config.keyValue.value 配置的value
 * @param {number} config.valueType 配置类型，1: NUMBER, 2: STRING, 3: TEXT, 4: JSON, 5: XML, 18: 日期, 20: yaml
 * @param {object} config.secretInfo 密钥信息
 * @param {string} config.secretInfo.appId 项目Id
 * @param {string} config.secretInfo.userId 用户Id
 * @param {string} config.secretInfo.secretKey 密钥
 * @param {string} config.secretInfo.envName 配置环境
 * @param {string} config.secretInfo.groupName 配置组
 * @returns {Promise<object>} 请求Promise
 *
 * @example
 * updateRainbowKV({
 *   keyValue: {
 *     key: 'theKey',
 *     value: 'theValue',
 *   },
 *   valueType: 2,
 *   secretInfo: {
 *     appId: 'xxx',
 *     userId: 'xxx',
 *     secretKey: 'xxx',
 *     envName: 'prod',
 *     groupName: 'xxx',
 *   }
 * }).then(() => {
 *
 * })
 */
function updateRainbowKV(_ref3) {
  var keyValue = _ref3.keyValue,
    _ref3$valueType = _ref3.valueType,
    valueType = _ref3$valueType === void 0 ? 2 : _ref3$valueType,
    secretInfo = _ref3.secretInfo;
  return baseRequestRainbow({
    url: '/adminapi.Config/ChangeKeyReq',
    data: {
      is_sync: false,
      key_values: [Object.assign({
        value_type: valueType,
        config_op_type: 1,
        description: ''
      }, keyValue)]
    },
    secretInfo: secretInfo
  });
}
/**
 * 创建发布任务
 *
 * @param {object} config 配置信息
 * @param {string} config.versionName 版本信息
 * @param {object} config.secretInfo 密钥信息
 * @param {string} config.secretInfo.appId 项目Id
 * @param {string} config.secretInfo.userId 用户Id
 * @param {string} config.secretInfo.secretKey 密钥
 * @param {string} config.secretInfo.envName 配置环境
 * @param {string} config.secretInfo.groupName 配置组
 * @returns {Promise<object>} 请求Promise
 *
 * @example
 * createRainbowPublishJob({
 *   versionName: 'version',
 *   secretInfo: {
 *     appId: 'xxx',
 *     userId: 'xxx',
 *     secretKey: 'xxx',
 *     envName: 'prod',
 *     groupName: 'xxx',
 *   }
 * }).then(() => {
 *
 * })
 */
function createRainbowPublishJob(_ref4) {
  var versionName = _ref4.versionName,
    secretInfo = _ref4.secretInfo,
    creator = _ref4.creator,
    approvers = _ref4.approvers,
    _ref4$type = _ref4.type,
    type = _ref4$type === void 0 ? 0 : _ref4$type;
  return baseRequestRainbow({
    url: '/adminapi.Config/CreateReleaseTaskReq',
    data: {
      creator: creator,
      approvers: approvers,
      version_name: versionName,
      type: type
    },
    secretInfo: secretInfo
  });
}
/**
 * 发布任务
 *
 * @param {object} config 配置信息
 * @param {string} config.taskId 任务Id
 * @param {object} config.secretInfo 密钥信息
 * @param {string} config.secretInfo.appId 项目Id
 * @param {string} config.secretInfo.userId 用户Id
 * @param {string} config.secretInfo.secretKey 密钥
 * @param {string} config.secretInfo.envName 配置环境
 * @param {string} config.secretInfo.groupName 配置组
 * @returns {Promise<object>} 请求Promise
 *
 * @example
 * publishRainbowTask({
 *   taskId: 'taskId',
 *   secretInfo: {
 *     appId: 'xxx',
 *     userId: 'xxx',
 *     secretKey: 'xxx',
 *     envName: 'prod',
 *     groupName: 'xxx',
 *   }
 * }).then(() => {
 *
 * })
 */
function publishRainbowTask(_ref5) {
  var taskId = _ref5.taskId,
    secretInfo = _ref5.secretInfo;
  return baseRequestRainbow({
    url: '/adminapi.Release/ReleaseMainTaskReq',
    data: {
      task_id: taskId
    },
    secretInfo: secretInfo
  });
}
/**
 * 关闭任务
 *
 * @param {object} config 配置信息
 * @param {string} config.taskId 任务Id
 * @param {object} config.secretInfo 密钥信息
 * @param {string} config.secretInfo.appId 项目Id
 * @param {string} config.secretInfo.userId 用户Id
 * @param {string} config.secretInfo.secretKey 密钥
 * @param {string} config.secretInfo.envName 配置环境
 * @param {string} config.secretInfo.groupName 配置组
 * @returns {Promise<object>} 请求Promise
 *
 * @example
 * closeRainbowTask({
 *   taskId: 'taskId',
 *   secretInfo: {
 *     appId: 'xxx',
 *     userId: 'xxx',
 *     secretKey: 'xxx',
 *     envName: 'prod',
 *     groupName: 'xxx',
 *   }
 * }).then(() => {
 *
 * })
 */
function closeRainbowTask(_ref6) {
  var taskId = _ref6.taskId,
    secretInfo = _ref6.secretInfo;
  return baseRequestRainbow({
    url: '/adminapi.Release/CloseReleaseTaskReq',
    data: {
      task_id: taskId
    },
    secretInfo: secretInfo
  });
}
/**
 * 更新或新增值并发布
 * @param {object} config 配置信息
 * @param {string} config.key 配置key
 * @param {string} config.value 配置value
 * @param {number} config.valueType 配置类型，1: NUMBER, 2: STRING, 3: TEXT, 4: JSON, 5: XML, 18: 日期, 20: yaml
 * @param {object} config.secretInfo 密钥信息
 * @param {string} config.secretInfo.appId 项目Id
 * @param {string} config.secretInfo.userId 用户Id
 * @param {string} config.secretInfo.secretKey 密钥
 * @param {string} config.secretInfo.envName 配置环境
 * @param {string} config.secretInfo.groupName 配置组
 * @returns {Promise<object>} 请求Promise
 *
 * @example
 * updateRainbowKVAndPublish({
 *   key: 'key',
 *   value: 'value',
 *   valueType: 2,
 *   secretInfo: {
 *     appId: 'xxx',
 *     userId: 'xxx',
 *     secretKey: 'xxx',
 *     envName: 'prod',
 *     groupName: 'xxx',
 *   }
 * }).then(() => {
 *
 * })
 */
function updateRainbowKVAndPublish(_ref7) {
  var key = _ref7.key,
    value = _ref7.value,
    valueType = _ref7.valueType,
    secretInfo = _ref7.secretInfo,
    creator = _ref7.creator,
    approvers = _ref7.approvers;
  return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    var versionName, taskRes, taskId, _t;
    return _regeneratorRuntime.wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          _context.prev = 0;
          _context.next = 1;
          return addOrUpdateRainbowKV({
            keyValue: {
              key: key,
              value: value
            },
            valueType: valueType,
            secretInfo: secretInfo
          });
        case 1:
          versionName = getVersion();
          _context.next = 2;
          return createRainbowPublishJob({
            creator: creator,
            approvers: approvers,
            versionName: versionName,
            secretInfo: secretInfo
          });
        case 2:
          taskRes = _context.sent;
          taskId = taskRes.task_id;
          _context.next = 3;
          return ApprovalRainbowReleaseTask({
            secretInfo: secretInfo,
            taskId: taskId,
            versionName: versionName
          });
        case 3:
          _context.next = 4;
          return publishRainbowTask({
            taskId: taskId,
            secretInfo: secretInfo
          });
        case 4:
          _context.next = 5;
          return closeRainbowTask({
            taskId: taskId,
            secretInfo: secretInfo
          });
        case 5:
          return _context.abrupt("return", taskRes);
        case 6:
          _context.prev = 6;
          _t = _context["catch"](0);
          return _context.abrupt("return", Promise.reject(_t));
        case 7:
        case "end":
          return _context.stop();
      }
    }, _callee, null, [[0, 6]]);
  }));
}
/**
 * 处理彩虹配置发布任务
 * @param {object} config 配置信息
 * @param {string} config.taskId 任务ID
 * @param {object} config.secretInfo 密钥信息
 * @param {string} config.secretInfo.appId 项目Id
 * @param {string} config.secretInfo.userId 用户Id
 * @param {string} config.secretInfo.secretKey 密钥
 * @param {string} config.secretInfo.envName 配置环境
 * @param {string} config.secretInfo.groupName 配置组
 * @param {string} config.versionName 版本名称
 * @returns {Promise<object>} 请求Promise
 *
 * @example
 * processRainbowReleaseTask({
 *   taskId: 'taskId',
 *   secretInfo: {
 *     appId: 'xxx',
 *     userId: 'xxx',
 *     secretKey: 'xxx',
 *     envName: 'prod',
 *     groupName: 'xxx',
 *   },
 *   versionName: 'version'
 * }).then(() => {
 *
 * })
 */
function processRainbowReleaseTask(_ref8) {
  var taskId = _ref8.taskId,
    secretInfo = _ref8.secretInfo,
    versionName = _ref8.versionName;
  return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
    var _t2;
    return _regeneratorRuntime.wrap(function (_context2) {
      while (1) switch (_context2.prev = _context2.next) {
        case 0:
          _context2.prev = 0;
          _context2.next = 1;
          return ApprovalRainbowReleaseTask({
            secretInfo: secretInfo,
            taskId: taskId,
            versionName: versionName
          });
        case 1:
          _context2.next = 2;
          return publishRainbowTask({
            taskId: taskId,
            secretInfo: secretInfo
          });
        case 2:
          _context2.next = 3;
          return closeRainbowTask({
            taskId: taskId,
            secretInfo: secretInfo
          });
        case 3:
          return _context2.abrupt("return", {
            success: true,
            taskId: taskId
          });
        case 4:
          _context2.prev = 4;
          _t2 = _context2["catch"](0);
          return _context2.abrupt("return", Promise.reject(_t2));
        case 5:
        case "end":
          return _context2.stop();
      }
    }, _callee2, null, [[0, 4]]);
  }));
}
/**
 * 查询分组配置
 * @param {object} config 配置信息
 * @param {object} config.secretInfo 密钥信息
 * @param {string} config.secretInfo.appId 项目Id
 * @param {string} config.secretInfo.userId 用户Id
 * @param {string} config.secretInfo.secretKey 密钥
 * @param {string} config.secretInfo.envName 配置环境
 * @param {string} config.secretInfo.groupName 配置组
 * @returns {Promise<Array<object>>} 分组配置
 *
 * @example
 * queryGroupInfo({
 *   secretInfo: {
 *     appId: 'xxx',
 *     userId: 'xxx',
 *     secretKey: 'xxx',
 *     envName: 'prod',
 *     groupName: 'xxx',
 *   }
 * }).then(() => {
 *
 * })
 */
function queryGroupInfo(_ref9) {
  var secretInfo = _ref9.secretInfo;
  return new Promise(function (resolve, reject) {
    baseRequestRainbow({
      url: '/adminapi.Config/QueryGroupInfoReq',
      data: {},
      secretInfo: secretInfo
    }).then(function (res) {
      var _a, _b;
      var keyValues = ((_b = (_a = res.config_infos) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.key_values) || [];
      resolve(keyValues);
    })["catch"](function (err) {
      reject(err);
    });
  });
}
function OneClickReleaseRainbowTask(_ref0) {
  var secretInfo = _ref0.secretInfo,
    versionName = _ref0.versionName,
    creator = _ref0.creator,
    updators = _ref0.updators,
    approvers = _ref0.approvers,
    _ref0$type = _ref0.type,
    type = _ref0$type === void 0 ? 0 : _ref0$type,
    _ref0$description = _ref0.description,
    description = _ref0$description === void 0 ? '' : _ref0$description;
  return baseRequestRainbow({
    url: '/adminapi.Config/OneClickReleaseTaskReq',
    data: {
      version_name: versionName,
      creator: creator,
      updators: updators,
      approvers: approvers,
      type: type,
      description: description
    },
    secretInfo: secretInfo
  });
}
function ApprovalRainbowReleaseTask(_ref1) {
  var secretInfo = _ref1.secretInfo,
    taskId = _ref1.taskId,
    versionName = _ref1.versionName,
    _ref1$status = _ref1.status,
    status = _ref1$status === void 0 ? 3 : _ref1$status,
    _ref1$rejectReason = _ref1.rejectReason,
    rejectReason = _ref1$rejectReason === void 0 ? '' : _ref1$rejectReason;
  return baseRequestRainbow({
    url: '/adminapi.Release/ApprovalReleaseTaskReq',
    data: {
      task_id: taskId,
      status: status,
      reject_reason: rejectReason,
      version_name: versionName
    },
    secretInfo: secretInfo
  });
}
/**
 * 更新彩虹配置并创建发布任务
 * @param {object} config 配置信息
 * @param {string} config.key 配置key
 * @param {string} config.value 配置value
 * @param {number} config.valueType 配置类型，1: NUMBER, 2: STRING, 3: TEXT, 4: JSON, 5: XML, 18: 日期, 20: yaml
 * @param {object} config.secretInfo 密钥信息
 * @param {string} config.secretInfo.appId 项目Id
 * @param {string} config.secretInfo.userId 用户Id
 * @param {string} config.secretInfo.secretKey 密钥
 * @param {string} config.secretInfo.envName 配置环境
 * @param {string} config.secretInfo.groupName 配置组
 * @param {string} config.creator 创建者
 * @param {string} config.approvers 审批者
 * @returns {Promise<{taskRes: any, versionName: string}>} 发布任务结果和版本名称
 *
 * @example
 * updateRainbowConfigAndCreateTask({
 *   key: 'configKey',
 *   value: 'configValue',
 *   valueType: 2,
 *   secretInfo: {
 *     appId: 'xxx',
 *     userId: 'xxx',
 *     secretKey: 'xxx',
 *     envName: 'prod',
 *     groupName: 'xxx',
 *   },
 *   creator: 'creatorName',
 *   approvers: 'approverName'
 * }).then(({ taskRes, versionName }) => {
 *   console.log('任务创建成功:', taskRes);
 *   console.log('版本名称:', versionName);
 * })
 */
function updateRainbowConfigAndCreateTask(_ref10) {
  var key = _ref10.key,
    value = _ref10.value,
    valueType = _ref10.valueType,
    secretInfo = _ref10.secretInfo,
    creator = _ref10.creator,
    approvers = _ref10.approvers;
  return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
    var versionName, taskRes;
    return _regeneratorRuntime.wrap(function (_context3) {
      while (1) switch (_context3.prev = _context3.next) {
        case 0:
          _context3.next = 1;
          return addOrUpdateRainbowKV({
            keyValue: {
              key: key,
              value: value
            },
            valueType: valueType,
            secretInfo: secretInfo
          });
        case 1:
          // 2. 获取版本号
          versionName = getVersion(); // 3. 创建发布任务
          _context3.next = 2;
          return createRainbowPublishJob({
            creator: creator,
            approvers: approvers,
            versionName: versionName,
            secretInfo: secretInfo
          });
        case 2:
          taskRes = _context3.sent;
          return _context3.abrupt("return", {
            taskRes: taskRes,
            versionName: versionName
          });
        case 3:
        case "end":
          return _context3.stop();
      }
    }, _callee3);
  }));
}

export { ApprovalRainbowReleaseTask, OneClickReleaseRainbowTask, addOrUpdateRainbowKV, addRainbowKV, closeRainbowTask, createRainbowPublishJob, processRainbowReleaseTask, publishRainbowTask, queryGroupInfo, updateRainbowConfigAndCreateTask, updateRainbowKV, updateRainbowKVAndPublish };
