(function (global, factory) {
  if (typeof define === "function" && define.amd) {
    define(["exports", "rxjs", "rxjs/operators", "../creators/io", "./send", "./consume"], factory);
  } else if (typeof exports !== "undefined") {
    factory(exports, require("rxjs"), require("rxjs/operators"), require("../creators/io"), require("./send"), require("./consume"));
  } else {
    var mod = {
      exports: {}
    };
    factory(mod.exports, global.rxjs, global.operators, global.io, global.send, global.consume);
    global.conduit = mod.exports;
  }
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _rxjs, _operators, _io2, _send2, _consume2) {
  "use strict";

  Object.defineProperty(_exports, "__esModule", {
    value: true
  });
  _exports.default = _exports.testExports = void 0;
  _io2 = _interopRequireDefault(_io2);
  _send2 = _interopRequireDefault(_send2);
  _consume2 = _interopRequireDefault(_consume2);

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

  const errors = {
    noUrl: new Error('conduit operator requires a {url<String>}')
  };

  function createMessageBuffer(ioEvent$) {
    return messageIn$ => {
      const ioEventSub$ = ioEvent$.pipe((0, _operators.share)());

      const closeBuffer = () => ioEventSub$.pipe((0, _operators.map)(([socket]) => socket && socket.connected), (0, _operators.pairwise)(), (0, _operators.filter)(([wasConnected, nowConnected]) => !wasConnected && nowConnected));

      const bufferedMessage$ = messageIn$.pipe((0, _operators.withLatestFrom)((0, _rxjs.merge)((0, _rxjs.of)([null, null]), ioEventSub$)), (0, _operators.filter)(([, [socket]]) => !socket || !socket.connected), (0, _operators.map)(([message]) => message), (0, _operators.bufferWhen)(closeBuffer), (0, _operators.mergeMap)(bufferedItems => (0, _rxjs.of)(...bufferedItems)));
      return bufferedMessage$;
    };
  }

  function passthroughMessageBuffer(ioEvent$) {
    return messageIn$ => messageIn$.pipe((0, _operators.withLatestFrom)((0, _rxjs.merge)((0, _rxjs.of)([null, null]), ioEvent$)), (0, _operators.filter)(([, [socket]]) => socket && socket.connected), (0, _operators.map)(([message]) => message));
  }

  function bufferMessages(ioEvent$, _createMessageBuffer = createMessageBuffer, _passthroughMessageBuffer = passthroughMessageBuffer) {
    return messageIn$ => {
      const messageInSub$ = messageIn$.pipe((0, _operators.share)());
      const ioEventSub$ = ioEvent$.pipe((0, _operators.share)());
      const bufferedMessage$ = messageInSub$.pipe(_createMessageBuffer(ioEventSub$));
      const unbufferedMessage$ = messageInSub$.pipe(_passthroughMessageBuffer(ioEventSub$));
      return (0, _rxjs.merge)(bufferedMessage$, unbufferedMessage$);
    };
  }

  const conduit = function conduit({
    url,
    socketOptions = {},
    stop$ = (0, _rxjs.of)(),
    _bufferMessages = bufferMessages,
    _send = _send2.default,
    _consume = _consume2.default,
    _io = _io2.default,
    bufferOnDisconnect = true
  }) {
    if (!url) return () => (0, _rxjs.throwError)(errors.noUrl);
    return messageIn$ => {
      const ioEvent$ = _io({
        url,
        socketOptions,
        stop$
      }).pipe((0, _operators.share)());

      const publisher$ = messageIn$.pipe(bufferOnDisconnect ? _bufferMessages(ioEvent$) : (0, _operators.tap)(() => true), _send({
        io$: ioEvent$
      }), (0, _operators.filter)(() => false));
      const consumer$ = ioEvent$.pipe(_consume());
      return (0, _rxjs.merge)(publisher$, consumer$).pipe((0, _operators.takeUntil)(stop$));
    };
  };

  const testExports = {
    createMessageBuffer,
    passthroughMessageBuffer
  };
  _exports.testExports = testExports;
  var _default = conduit;
  _exports.default = _default;
});
//# sourceMappingURL=conduit.js.map