'use strict';

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

var _regeneratorRuntime = require('@babel/runtime/regenerator');
var tslib_es6 = require('../tslib.es6-0d92ef81.js');
var nodejs_path = require('../nodejs/path.js');
var wecomRobot_base = require('./base.js');
var nodeImg_img = require('../node-img/img.js');
require('./helper.js');
require('axios');
require('../nodejs/crypto.js');
require('../nodejs/fs.js');
require('../nodejs/request.js');

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

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

/**
 * 发送企业微信机器人base64图片，其实就是先保存到本地，然后生成md5，最后发送
 * @param {object} config 配置信息
 * @param {string} config.img base64图片
 * @param {string} config.chatId 会话Id
 * @param {string} config.webhookUrl webhook地址
 * @returns {Promise<object>} 请求Promise
 * @example
 *
 * sendWxRobotBase64Img({
 *   img: 'xxx',
 *   chatId: 'xxx',
 *   webhookUrl: 'xxx',
 * }).then(() => {
 *
 * })
 *
 */
function sendWxRobotBase64Img(_ref) {
  var img = _ref.img,
    chatId = _ref.chatId,
    webhookUrl = _ref.webhookUrl;
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() {
    var saveFilePath, pureSrc, md5Val;
    return _regeneratorRuntime__default["default"].wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          if (!(!img || !webhookUrl)) {
            _context.next = 1;
            break;
          }
          return _context.abrupt("return", Promise.reject('参数不全'));
        case 1:
          saveFilePath = nodejs_path.getPath().resolve(__dirname, '.temp.png');
          _context.next = 2;
          return nodeImg_img.saveBase64ImgToFile({
            imgUrl: img,
            savePath: saveFilePath
          });
        case 2:
          pureSrc = _context.sent;
          _context.next = 3;
          return nodeImg_img.getImgMd5({
            savePath: saveFilePath
          });
        case 3:
          md5Val = _context.sent;
          _context.next = 4;
          return wecomRobot_base.sendWxRobotImg({
            webhookUrl: webhookUrl,
            chatId: chatId,
            content: pureSrc,
            md5Val: md5Val
          });
        case 4:
          return _context.abrupt("return", _context.sent);
        case 5:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
}

exports.sendWxRobotBase64Img = sendWxRobotBase64Img;
