'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _regeneratorRuntime = require('@babel/runtime/regenerator');
var tslib_es6 = require('../tslib.es6-0d92ef81.js');
var rainbowToCos_api = require('./api.js');
var rainbowToCos_config = require('./config.js');
var rainbowToCos_robotMsg = require('./robot-msg.js');
var rainbowToCos_syncToCos = require('./sync-to-cos.js');
require('../rainbow/rainbow-admin.js');
require('../rainbow/helper/helper.js');
require('../rainbow/helper/rainbow-base-request.js');
require('axios');
require('../rainbow/helper/rainbow-signature.js');
require('../nodejs/crypto.js');
require('../rainbow/rainbow-user.js');
require('../rainbow/sdk.js');
require('../json/json-parse.js');
require('../list/flat.js');
require('./helper/helper.js');
require('../nodejs/path.js');
require('../tencent-cloud/cos/link.js');
require('./helper/value-type.js');
require('../string/string.js');
require('../fs/fs-util.js');
require('@babel/runtime/helpers/toConsumableArray');
require('../fs/fs.js');
require('../nodejs/fs.js');
require('../nodejs/os.js');
require('../time/time.js');
require('../wecom-robot/batch-send.js');
require('../wecom-robot/base.js');
require('../wecom-robot/helper.js');
require('../wecom-robot/send-img.js');
require('../node-img/img.js');
require('../nodejs/request.js');
require('../tencent-cloud/cos/cos.js');
require('../tencent-cloud/cos/helper.js');
require('../bite/format-bite.js');
require('../third-party/cos-nodejs-sdk-v5.js');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _regeneratorRuntime__default = /*#__PURE__*/_interopDefaultLegacy(_regeneratorRuntime);

/**
 * 监听rainbow，同步到cos，并发送到机器人
 * @param {object} options 配置
 * @param {object} options.rainbowSecretInfo 七彩石密钥信息
 * @param {object} options.cosInfo 腾讯云信息
 * @param {string} options.appName 七彩石项目名称
 *
 * @param {string} options.webhookUrl 机器人回调
 * @param {string} options.chatId 会话id
 * @param {0|1|2} options.sendToRobotType 发送机器人类型，0 不发送，1 发送变化的部分，2 全部发送
 *
 * @example
 *
 * await watchRainbowToCosAndSendRobot({
 *   rainbowSecretInfo: {
 *     appID: RAINBOW_OPEN_APP_ID,
 *     userID: RAINBOW_OPEN_YGW_USER_ID,
 *     secretKey: RAINBOW_OPEN_YGW_SECRET_KEY,
 *     envName: 'Default',
 *     groupName: 'group',
 *   },
 *   appName: 'configApp',
 *   cosInfo: {
 *     secretId,
 *     secretKey,
 *     bucket: 'bucket',
 *     region: 'ap-guangzhou',
 *     dir: 'rb',
 *   },
 *   webhookUrl: 'xxx',
 *   chatId: 'xxx',
 *   sendToRobotType: 1,
 * });
 *
 */
function watchRainbowToCosAndSendRobot(_ref) {
  var secretInfo = _ref.rainbowSecretInfo,
    cosInfo = _ref.cosInfo,
    appName = _ref.appName,
    webhookUrl = _ref.webhookUrl,
    chatId = _ref.chatId,
    _ref$sendToRobotType = _ref.sendToRobotType,
    sendToRobotType = _ref$sendToRobotType === void 0 ? rainbowToCos_config.SendToRobotTypeMap.NO_SEND : _ref$sendToRobotType;
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() {
    var _yield$fetchLatestRai, config, originConfig, equal;
    return _regeneratorRuntime__default["default"].wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          _context.next = 1;
          return rainbowToCos_api.fetchLatestRainbowData({
            secretInfo: secretInfo,
            appName: appName
          });
        case 1:
          _yield$fetchLatestRai = _context.sent;
          config = _yield$fetchLatestRai.config;
          originConfig = _yield$fetchLatestRai.originConfig;
          equal = _yield$fetchLatestRai.equal;
          if (equal) {
            _context.next = 2;
            break;
          }
          _context.next = 2;
          return rainbowToCos_syncToCos.syncRainbowToCOS({
            configList: config,
            secretInfo: secretInfo,
            appName: appName,
            cosInfo: cosInfo
          });
        case 2:
          if (sendToRobotType) {
            _context.next = 3;
            break;
          }
          return _context.abrupt("return");
        case 3:
          _context.next = 4;
          return rainbowToCos_robotMsg.sendRainbowInfoToRobot({
            newConfig: config,
            originConfig: originConfig,
            secretInfo: secretInfo,
            cosInfo: cosInfo,
            appName: appName,
            webhookUrl: webhookUrl,
            chatId: chatId,
            isGenAll: sendToRobotType === 2
          });
        case 4:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
}

exports.watchRainbowToCosAndSendRobot = watchRainbowToCosAndSendRobot;
