(function (global, factory) {
  if (typeof define === "function" && define.amd) {
    define(["exports", "aws-sdk", "rxjs", "rxjs/operators", "../internals/createMultipartUpload", "../internals/uploadPart", "../internals/completeUpload"], factory);
  } else if (typeof exports !== "undefined") {
    factory(exports, require("aws-sdk"), require("rxjs"), require("rxjs/operators"), require("../internals/createMultipartUpload"), require("../internals/uploadPart"), require("../internals/completeUpload"));
  } else {
    var mod = {
      exports: {}
    };
    factory(mod.exports, global.awsSdk, global.rxjs, global.operators, global.createMultipartUpload, global.uploadPart, global.completeUpload);
    global.upload = mod.exports;
  }
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _awsSdk, _rxjs, _operators, _createMultipartUpload2, _uploadPart2, _completeUpload2) {
  "use strict";

  Object.defineProperty(_exports, "__esModule", {
    value: true
  });
  _exports.default = void 0;
  _awsSdk = _interopRequireDefault(_awsSdk);
  _createMultipartUpload2 = _interopRequireDefault(_createMultipartUpload2);
  _uploadPart2 = _interopRequireDefault(_uploadPart2);
  _completeUpload2 = _interopRequireDefault(_completeUpload2);

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

  const s3Default = new _awsSdk.default.S3();

  const upload = function upload({
    s3Key,
    s3Bucket,
    contentType,
    s3 = s3Default,
    _createMultipartUpload = _createMultipartUpload2.default,
    _uploadPart = _uploadPart2.default,
    _completeUpload = _completeUpload2.default
  }) {
    return source$ => {
      const uploadId$ = new _rxjs.AsyncSubject();

      _createMultipartUpload({
        s3Key,
        s3Bucket,
        contentType,
        s3
      }).subscribe(uploadId$);

      const parts$ = source$.pipe((0, _operators.withLatestFrom)(uploadId$), (0, _operators.scan)((memo, [filePart, uploadId]) => [uploadId, filePart, memo[2] + 1], [null, null, 0]), (0, _operators.mergeMap)(([uploadId, partBuffer, partNumber]) => (0, _rxjs.zip)((0, _rxjs.of)(partNumber), _uploadPart({
        uploadId,
        partNumber,
        partBuffer,
        s3Bucket,
        s3Key,
        s3
      }))), (0, _operators.map)(([partNumber, response]) => [partNumber, response.ETag]), (0, _operators.toArray)());
      const complete$ = parts$.pipe((0, _operators.withLatestFrom)(uploadId$), (0, _operators.mergeMap)(([parts, uploadId]) => _completeUpload({
        parts,
        uploadId,
        s3Bucket,
        s3Key,
        s3
      })));
      return complete$;
    };
  };

  var _default = upload;
  _exports.default = _default;
});
//# sourceMappingURL=upload.js.map