'use strict';

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

var _regeneratorRuntime = require('@babel/runtime/regenerator');
var tslib_es6 = require('../tslib.es6-0d92ef81.js');
var axios = require('axios');
var nodejs_fs = require('../nodejs/fs.js');
var thirdParty_formData = require('../third-party/form-data.js');
var thirdParty_qs = require('../third-party/qs.js');

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

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

function createTencentDoc(_ref) {
  var accessToken = _ref.accessToken,
    clientId = _ref.clientId,
    openId = _ref.openId,
    type = _ref.type,
    title = _ref.title,
    _ref$folderId = _ref.folderId,
    folderId = _ref$folderId === void 0 ? '/' : _ref$folderId;
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() {
    var qs, res;
    return _regeneratorRuntime__default["default"].wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          qs = thirdParty_qs.getQs();
          _context.next = 1;
          return axios__default["default"]({
            method: 'POST',
            url: 'https://docs.qq.com/openapi/drive/v2/files',
            headers: {
              'Access-Token': accessToken,
              'Client-Id': clientId,
              'Open-Id': openId,
              'Content-Type': 'application/x-www-form-urlencoded'
            },
            data: qs.stringify({
              type: type,
              title: title,
              folderId: folderId
            })
          });
        case 1:
          res = _context.sent;
          return _context.abrupt("return", res.data);
        case 2:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
}
function convertTencentFileId(_ref2) {
  var accessToken = _ref2.accessToken,
    clientId = _ref2.clientId,
    openId = _ref2.openId,
    type = _ref2.type,
    value = _ref2.value;
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee2() {
    var result;
    return _regeneratorRuntime__default["default"].wrap(function (_context2) {
      while (1) switch (_context2.prev = _context2.next) {
        case 0:
          _context2.next = 1;
          return axios__default["default"]({
            method: 'GET',
            url: "https://docs.qq.com/openapi/drive/v2/util/converter?type=".concat(type, "&value=").concat(value),
            headers: {
              'Access-Token': accessToken,
              'Client-Id': clientId,
              'Open-Id': openId
            }
          });
        case 1:
          result = _context2.sent;
          return _context2.abrupt("return", result.data);
        case 2:
        case "end":
          return _context2.stop();
      }
    }, _callee2);
  }));
}
function uploadTencentDocImage(_ref3) {
  var accessToken = _ref3.accessToken,
    clientId = _ref3.clientId,
    openId = _ref3.openId,
    image = _ref3.image;
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee3() {
    var FormData, formData, len, result;
    return _regeneratorRuntime__default["default"].wrap(function (_context3) {
      while (1) switch (_context3.prev = _context3.next) {
        case 0:
          FormData = thirdParty_formData.getFormData();
          formData = new FormData();
          formData.append('image', nodejs_fs.getFs().createReadStream(image));
          // eslint-disable-next-line max-len
          _context3.next = 1;
          return new Promise(function (resolve, reject) {
            return formData.getLength(function (err, length) {
              return err ? reject(err) : resolve(length);
            });
          });
        case 1:
          len = _context3.sent;
          _context3.next = 2;
          return axios__default["default"]({
            method: 'POST',
            url: 'https://docs.qq.com/openapi/resources/v2/images',
            headers: Object.assign({
              'Access-Token': accessToken,
              'Client-Id': clientId,
              'Open-Id': openId,
              'Content-Type': 'application/json',
              'Content-Length': len
            }, formData.getHeaders()),
            data: formData
          });
        case 2:
          result = _context3.sent;
          return _context3.abrupt("return", result.data);
        case 3:
        case "end":
          return _context3.stop();
      }
    }, _callee3);
  }));
}
function asyncExportTencentDoc(_ref4) {
  var accessToken = _ref4.accessToken,
    clientId = _ref4.clientId,
    openId = _ref4.openId,
    fileId = _ref4.fileId,
    exportType = _ref4.exportType;
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee4() {
    var qs, result;
    return _regeneratorRuntime__default["default"].wrap(function (_context4) {
      while (1) switch (_context4.prev = _context4.next) {
        case 0:
          qs = thirdParty_qs.getQs();
          _context4.next = 1;
          return axios__default["default"]({
            method: 'POST',
            url: "https://docs.qq.com/openapi/drive/v2/files/".concat(fileId, "/async-export"),
            headers: {
              'Access-Token': accessToken,
              'Client-Id': clientId,
              'Open-Id': openId,
              'Content-Type': 'application/x-www-form-urlencoded'
            },
            data: qs.stringify({
              exportType: exportType
            })
          });
        case 1:
          result = _context4.sent;
          return _context4.abrupt("return", result.data);
        case 2:
        case "end":
          return _context4.stop();
      }
    }, _callee4);
  }));
}
function checkExportTencentDocProgress(_ref5) {
  var accessToken = _ref5.accessToken,
    clientId = _ref5.clientId,
    openId = _ref5.openId,
    fileId = _ref5.fileId,
    operationId = _ref5.operationId;
  return tslib_es6.__awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee5() {
    var result;
    return _regeneratorRuntime__default["default"].wrap(function (_context5) {
      while (1) switch (_context5.prev = _context5.next) {
        case 0:
          _context5.next = 1;
          return axios__default["default"]({
            method: 'GET',
            url: "https://docs.qq.com/openapi/drive/v2/files/".concat(fileId, "/export-progress?operationID=").concat(operationId),
            headers: {
              'Access-Token': accessToken,
              'Client-Id': clientId,
              'Open-Id': openId,
              'Content-Type': 'application/json'
            }
          });
        case 1:
          result = _context5.sent;
          return _context5.abrupt("return", result.data);
        case 2:
        case "end":
          return _context5.stop();
      }
    }, _callee5);
  }));
}

exports.asyncExportTencentDoc = asyncExportTencentDoc;
exports.checkExportTencentDocProgress = checkExportTencentDocProgress;
exports.convertTencentFileId = convertTencentFileId;
exports.createTencentDoc = createTencentDoc;
exports.uploadTencentDocImage = uploadTencentDocImage;
