(function (global, factory) {
  if (typeof define === "function" && define.amd) {
    define(["exports", "rxjs", "rxjs/operators", "./ws", "../internals/actions"], factory);
  } else if (typeof exports !== "undefined") {
    factory(exports, require("rxjs"), require("rxjs/operators"), require("./ws"), require("../internals/actions"));
  } else {
    var mod = {
      exports: {}
    };
    factory(mod.exports, global.rxjs, global.operators, global.ws, global.actions);
    global.wsManager = mod.exports;
  }
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _rxjs, _operators, _ws2, actions) {
  "use strict";

  Object.defineProperty(_exports, "__esModule", {
    value: true
  });
  _exports.default = void 0;
  _ws2 = _interopRequireDefault(_ws2);
  actions = _interopRequireWildcard(actions);

  function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

  function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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

  const managedWs = function managedWs({
    url,
    socketOptions = {},
    stop$ = new _rxjs.Observable(),
    reconnectInterval = 3000,
    connectionTimeout = 3000,
    _ws = _ws2.default,
    _actions = actions
  }) {
    const disconnection$ = new _rxjs.BehaviorSubject();
    const ws$ = disconnection$.pipe((0, _operators.scan)(([index]) => [index + 1], [-1]), (0, _operators.mergeMap)(([index]) => (0, _rxjs.timer)(index === 0 ? 0 : reconnectInterval).pipe((0, _operators.mapTo)([(0, _ws2.default)({
      url,
      socketOptions
    }), _actions.reconnect(index)]), (0, _operators.mergeMap)(([_ws$, reconnectingAction]) => _ws$.pipe((0, _operators.map)(([socket, action]) => [socket, action]))))), (0, _operators.takeUntil)(stop$), (0, _operators.tap)(([, event]) => event.type === _actions.DISCONNECT ? disconnection$.next(event) : null));
    return ws$;
  };

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