'use strict';

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

var _regeneratorRuntime = require('@babel/runtime/regenerator');
var tslib_es6 = require('../tslib.es6-0d92ef81.js');
var wecomRobot_helper = require('./helper.js');
require('axios');

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

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

/**
 * 给机器人发送普通消息
 * @param {Object} config 配置内容
 * @param {string} config.webhookUrl - 钩子链接
 * @param {string} config.chatId - 会话id
 * @param {string} config.alias - 别名
 * @param {string} config.content - 内容
 * @returns {Promise<object>} Promise
 *
 * @example
 *
 * sendWxRobotMsg({
 *   webhookUrl: 'xxx',
 *   chatId: 'xxx',
 *   content: 'xxx',
 *   alias: 'xxx',
 * }).then(() => {
 *
 * })
 */
function sendWxRobotMsg(_ref) {
  var webhookUrl = _ref.webhookUrl,
    chatId = _ref.chatId,
    alias = _ref.alias,
    content = _ref.content;
  return new Promise(function (resolve, reject) {
    wecomRobot_helper.sendToRobot({
      webhookUrl: webhookUrl,
      params: {
        chatid: chatId,
        msgtype: 'text',
        text: {
          content: content,
          mentioned_list: Array.isArray(alias) ? alias : [alias]
        }
      }
    }).then(function (res) {
      resolve(res);
    })["catch"](function (err) {
      reject(err);
    });
  });
}
/**
 * 给机器人发送Markdown消息
 * @param {Object} config 配置内容
 * @param {string} config.webhookUrl - 钩子链接
 * @param {string} config.chatId - 会话id
 * @param {string} config.content - 内容
 * @param {Array<object>} config.attachments - 附加内容
 * @returns {Promise<object>} 请求Promise
 * @example
 *
 * sendWxRobotMarkdown({
 *   webhookUrl: 'xxx',
 *   chatId: 'xxx',
 *   content: 'xxx',
 *   attachments: []
 * }).then(() => {
 *
 * })
 */
function sendWxRobotMarkdown(_ref2) {
  var webhookUrl = _ref2.webhookUrl,
    chatId = _ref2.chatId,
    content = _ref2.content,
    attachments = _ref2.attachments,
    _ref2$isV = _ref2.isV2,
    isV2 = _ref2$isV === void 0 ? false : _ref2$isV;
  return new Promise(function (resolve, reject) {
    wecomRobot_helper.sendToRobot({
      webhookUrl: webhookUrl,
      params: {
        chatid: chatId,
        msgtype: isV2 ? 'markdown_v2' : 'markdown',
        markdown: isV2 ? undefined : {
          content: content,
          attachments: attachments,
          at_short_name: true
        },
        markdown_v2: isV2 ? {
          content: content
        } : undefined
      }
    }).then(function (res) {
      resolve(res);
    })["catch"](function (err) {
      reject(err);
    });
  });
}
/**
 * 给机器人发送图片
 * @param {Object} config 配置参数
 * @param {string} config.webhookUrl 钩子链接
 * @param {string} config.chatId 会话id
 * @param {string} config.content 内容
 * @param {string} config.md5Val md5内容
 * @returns {Promise<object>} 请求Promise
 *
 * @example
 *
 * sendWxRobotImg({
 *   webhookUrl: 'xxx',
 *   chatId: 'xxx',
 *   content: 'xxx',
 *   md5Val: 'xxx'
 * }).then(() => {
 *
 * })
 */
function sendWxRobotImg(_ref3) {
  var webhookUrl = _ref3.webhookUrl,
    chatId = _ref3.chatId,
    content = _ref3.content,
    md5Val = _ref3.md5Val;
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() {
    return _regeneratorRuntime__default["default"].wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          return _context.abrupt("return", new Promise(function (resolve, reject) {
            wecomRobot_helper.sendToRobot({
              webhookUrl: webhookUrl,
              params: {
                chatid: chatId,
                msgtype: 'image',
                image: {
                  base64: content,
                  md5: md5Val
                }
              }
            }).then(function (res) {
              resolve(res);
            })["catch"](function (err) {
              console.log('[sendWxRobotImg] err:\n ', err === null || err === void 0 ? void 0 : err.data);
              reject(err);
            });
          }));
        case 1:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
}

exports.sendWxRobotImg = sendWxRobotImg;
exports.sendWxRobotMarkdown = sendWxRobotMarkdown;
exports.sendWxRobotMsg = sendWxRobotMsg;
