import _regeneratorRuntime from '@babel/runtime/regenerator';
import { _ as __awaiter } from '../tslib.es6-848120af.js';
import { asyncExportTencentDoc, checkExportTencentDocProgress } from './tencent-doc.mjs';
import 'axios';
import '../nodejs/fs.mjs';
import '../third-party/form-data.mjs';
import '../third-party/qs.mjs';

function checkUrl(_ref) {
  var accessToken = _ref.accessToken,
    clientId = _ref.clientId,
    openId = _ref.openId,
    fileId = _ref.fileId,
    operationId = _ref.operationId;
  return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
    var res;
    return _regeneratorRuntime.wrap(function (_context) {
      while (1) switch (_context.prev = _context.next) {
        case 0:
          _context.next = 1;
          return checkExportTencentDocProgress({
            accessToken: accessToken,
            clientId: clientId,
            openId: openId,
            fileId: fileId,
            operationId: operationId
          });
        case 1:
          res = _context.sent;
          return _context.abrupt("return", res.data);
        case 2:
        case "end":
          return _context.stop();
      }
    }, _callee);
  }));
}
var holding = function holding(time) {
  return new Promise(function (resolve) {
    setTimeout(function () {
      return resolve(time);
    }, time);
  });
};
function pollCheckUrl(_ref2) {
  var accessToken = _ref2.accessToken,
    clientId = _ref2.clientId,
    openId = _ref2.openId,
    fileId = _ref2.fileId,
    operationId = _ref2.operationId,
    waitTime = _ref2.waitTime;
  return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
    var res;
    return _regeneratorRuntime.wrap(function (_context2) {
      while (1) switch (_context2.prev = _context2.next) {
        case 0:
          _context2.next = 1;
          return holding(waitTime);
        case 1:
          _context2.next = 2;
          return checkUrl({
            accessToken: accessToken,
            clientId: clientId,
            openId: openId,
            fileId: fileId,
            operationId: operationId
          });
        case 2:
          res = _context2.sent;
          console.log('[checkUrl] res: ', res);
          if (!res.url) {
            _context2.next = 3;
            break;
          }
          return _context2.abrupt("return", res.url);
        case 3:
          return _context2.abrupt("return", pollCheckUrl({
            accessToken: accessToken,
            clientId: clientId,
            openId: openId,
            fileId: fileId,
            operationId: operationId,
            waitTime: waitTime
          }));
        case 4:
        case "end":
          return _context2.stop();
      }
    }, _callee2);
  }));
}
function exportTencentDoc(_ref3) {
  var accessToken = _ref3.accessToken,
    clientId = _ref3.clientId,
    openId = _ref3.openId,
    fileId = _ref3.fileId,
    exportType = _ref3.exportType,
    _ref3$waitTime = _ref3.waitTime,
    waitTime = _ref3$waitTime === void 0 ? 0 : _ref3$waitTime;
  return __awaiter(this, void 0, void 0, /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
    var data, _ref4, operationId;
    return _regeneratorRuntime.wrap(function (_context3) {
      while (1) switch (_context3.prev = _context3.next) {
        case 0:
          _context3.next = 1;
          return asyncExportTencentDoc({
            accessToken: accessToken,
            clientId: clientId,
            openId: openId,
            fileId: fileId,
            exportType: exportType
          });
        case 1:
          data = _context3.sent;
          console.log('[asyncExportTencentDoc] data: ', data);
          _ref4 = data.data || {}, operationId = _ref4.operationID;
          console.log('[exportTencentDoc] operationId: ', operationId);
          if (operationId) {
            _context3.next = 2;
            break;
          }
          return _context3.abrupt("return");
        case 2:
          return _context3.abrupt("return", pollCheckUrl({
            accessToken: accessToken,
            clientId: clientId,
            openId: openId,
            fileId: fileId,
            operationId: operationId,
            waitTime: waitTime
          }));
        case 3:
        case "end":
          return _context3.stop();
      }
    }, _callee3);
  }));
}

export { exportTencentDoc };
