"use strict";

require("core-js/modules/es.promise.js");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.Uploader = void 0;

require("regenerator-runtime/runtime.js");

require("core-js/modules/es.array.for-each.js");

require("core-js/modules/es.object.to-string.js");

require("core-js/modules/web.dom-collections.for-each.js");

require("core-js/modules/es.object.keys.js");

require("core-js/modules/es.object.define-property.js");

var _axios = _interopRequireDefault(require("axios"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }

var Uploader = /*#__PURE__*/function () {
  function Uploader() {
    _classCallCheck(this, Uploader);
  }
  /**
   * Upload file to the pre-signed url.
   *
   * @param file File to be uploaded.
   * @param url Url for file upload.
   * @param fields AWS provided authentication fields
   * @param setUploadProgressPercent Callback to update upload progress.
   * @param cancelToken Token to cancel request.
   */


  _createClass(Uploader, null, [{
    key: "postUploadFile",
    value: function () {
      var _postUploadFile = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(file, url, fields, setUploadProgressPercent, cancelToken) {
        var formData;
        return regeneratorRuntime.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                formData = new FormData();
                Object.keys(fields).forEach(function (key) {
                  formData.append(key, fields[key]);
                }); // Actual file has to be appended last.

                formData.append("file", file);
                _context.next = 5;
                return _axios["default"].post(url, formData, {
                  headers: {
                    "Content-Type": ""
                  },
                  onUploadProgress: function onUploadProgress(p) {
                    setUploadProgressPercent(Math.round(p.loaded / p.total * 100));
                  },
                  cancelToken: cancelToken
                });

              case 5:
              case "end":
                return _context.stop();
            }
          }
        }, _callee);
      }));

      function postUploadFile(_x, _x2, _x3, _x4, _x5) {
        return _postUploadFile.apply(this, arguments);
      }

      return postUploadFile;
    }()
    /**
     * Upload file to the pre-signed url.
     *
     * @param file File to be uploaded.
     * @param url Url for file upload.
     * @param setUploadProgressPercent Callback to update upload progress.
     * @param cancelToken Token to cancel request.
     */

  }, {
    key: "putUploadFile",
    value: function () {
      var _putUploadFile = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(file, url, setUploadProgressPercent, cancelToken) {
        return regeneratorRuntime.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                _context2.next = 2;
                return _axios["default"].put(url, file, {
                  headers: {
                    "Content-Type": ""
                  },
                  onUploadProgress: function onUploadProgress(p) {
                    setUploadProgressPercent(Math.round(p.loaded / p.total * 100));
                  },
                  cancelToken: cancelToken
                });

              case 2:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2);
      }));

      function putUploadFile(_x6, _x7, _x8, _x9) {
        return _putUploadFile.apply(this, arguments);
      }

      return putUploadFile;
    }()
  }]);

  return Uploader;
}();

exports.Uploader = Uploader;
//# sourceMappingURL=data:application/json;charset=utf-8;base64, 