(function (global, factory) {
  if (typeof define === "function" && define.amd) {
    define(["exports", "rxjs", "rxjs/operators", "./mean", "./stdev"], factory);
  } else if (typeof exports !== "undefined") {
    factory(exports, require("rxjs"), require("rxjs/operators"), require("./mean"), require("./stdev"));
  } else {
    var mod = {
      exports: {}
    };
    factory(mod.exports, global.rxjs, global.operators, global.mean, global.stdev);
    global.dirtyZScore = mod.exports;
  }
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _rxjs, _operators, _mean2, _stdev2) {
  "use strict";

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

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

  function meanOp(state) {
    if (state && state.trueMean) return (0, _operators.mapTo)(state.trueMean);
    if (state && state.meanState) return (0, _mean2.default)(state.meanState);
    return (0, _mean2.default)();
  }

  function stdevOp(state) {
    if (state && state.trueStdev) return (0, _operators.mapTo)(state.trueStdev);
    if (state && state.stdevState) return (0, _stdev2.default)(state.stdevState);
    return (0, _stdev2.default)();
  }

  const dirtyZScore = function dirtyZScore(initialState) {
    return source$ => {
      const skipCount = initialState && initialState.stdevState && initialState.stdevState > 0 ? 0 : 1;
      const sourceSub$ = source$.pipe((0, _operators.share)());
      const mean$ = sourceSub$.pipe(meanOp(initialState), (0, _operators.skip)(skipCount));
      const stdev$ = sourceSub$.pipe(stdevOp(initialState));
      const zScore$ = (0, _rxjs.zip)(sourceSub$.pipe((0, _operators.skip)(skipCount)), mean$, stdev$).pipe((0, _operators.map)(([instanceVal, _mean, _stdev]) => (instanceVal - _mean) / _stdev));
      return zScore$;
    };
  };

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