import { __spreadArray as E, __read as j } from "./index-tslib.es6.js";
import { innerFrom as _ } from "./index-innerFrom.js";
import { Subject as y } from "./index-Subject.js";
import { SafeSubscriber as g } from "./index-Subscriber.js";
import { operate as A } from "./index-lift.js";
function z(r) {
  r === void 0 && (r = {});
  var t = r.connector, c = t === void 0 ? function() {
    return new y();
  } : t, e = r.resetOnError, a = e === void 0 ? !0 : e, C = r.resetOnComplete, x = C === void 0 ? !0 : C, O = r.resetOnRefCountZero, R = O === void 0 ? !0 : O;
  return function(S) {
    var o, n, u, f = 0, s = !1, v = !1, d = function() {
      n == null || n.unsubscribe(), n = void 0;
    }, m = function() {
      d(), o = u = void 0, s = v = !1;
    }, w = function() {
      var i = o;
      m(), i == null || i.unsubscribe();
    };
    return A(function(i, h) {
      f++, !v && !s && d();
      var b = u = u ?? c();
      h.add(function() {
        f--, f === 0 && !v && !s && (n = p(w, R));
      }), b.subscribe(h), !o && f > 0 && (o = new g({
        next: function(l) {
          return b.next(l);
        },
        error: function(l) {
          v = !0, d(), n = p(m, a, l), b.error(l);
        },
        complete: function() {
          s = !0, d(), n = p(m, x), b.complete();
        }
      }), _(i).subscribe(o));
    })(S);
  };
}
function p(r, t) {
  for (var c = [], e = 2; e < arguments.length; e++)
    c[e - 2] = arguments[e];
  if (t === !0) {
    r();
    return;
  }
  if (t !== !1) {
    var a = new g({
      next: function() {
        a.unsubscribe(), r();
      }
    });
    return _(t.apply(void 0, E([], j(c)))).subscribe(a);
  }
}
export {
  z as share
};
