import { shallowRef as ee, ref as O, onScopeDispose as te, watch as ne, readonly as M, toValue as Q } from "vue";
function ae(l) {
  return l instanceof Error ? l : new Error(typeof l == "string" ? l : JSON.stringify(l));
}
function re(l, W, r) {
  const a = {
    minInterval: r.minInterval,
    minDelay: r.minDelay ?? 0,
    autoStart: r.autoStart ?? !0,
    triggerOnResume: r.triggerOnResume ?? !1,
    pauseOnError: r.pauseOnError ?? !1,
    maxInFlightRequests: Math.max(1, r.maxInFlightRequests ?? 1),
    debounce: Math.max(0, r.debounce ?? 0)
  }, p = () => Math.max(0, Q(a.minInterval)), j = () => {
    const e = a.minDelay === void 0 ? void 0 : Q(a.minDelay);
    return Math.max(0, e ?? 0);
  }, u = () => p() > 0, i = ee({ status: "idle" }), w = O(null), t = O(!1);
  let E = 0, S = 0;
  const h = O(0);
  let c = !1, o = null, d = null, f = 0, m = 0;
  const A = /* @__PURE__ */ new Map(), I = /* @__PURE__ */ new Map(), g = [];
  let q, y = !1;
  const z = (e) => {
    q = e, y = !0;
  }, G = () => {
    if (i.value.status === "synced" || i.value.status === "stale") {
      const { value: e } = i.value;
      i.value = { status: "stale", value: e };
    }
  }, V = () => {
    if (g.length === 0) return;
    const e = g.shift();
    e == null || e.resolve();
  }, H = async () => {
    h.value < a.maxInFlightRequests || await new Promise((e) => {
      g.push({ resolve: e });
    });
  }, k = () => {
    o !== null && (clearTimeout(o), o = null);
  }, R = () => {
    d !== null && (clearTimeout(d), d = null);
  }, T = (e) => {
    A.forEach((n, s) => {
      n.signal.aborted || (I.set(s, e), n.abort());
    });
  }, K = (e = 0) => {
    const n = Date.now(), s = Math.max(f, m), F = s > n ? s - n : 0;
    return Math.max(0, e, F);
  }, D = (e = 0, n = "normal") => {
    if (!t.value || !u() || c) return;
    const s = K(e);
    o !== null && clearTimeout(o), o = setTimeout(() => {
      o = null, L(n);
    }, s);
  }, L = async (e) => {
    if (!t.value || c || !u()) return;
    const n = Date.now(), s = Math.max(f, m);
    if (n < s) {
      D(s - n, e);
      return;
    }
    if (!y) return;
    const F = q, X = S;
    if (await H(), !t.value || c || !u()) {
      V();
      return;
    }
    const b = new AbortController(), v = ++E;
    A.set(v, b), h.value += 1;
    const Y = p(), Z = Date.now();
    f = Math.max(f, Z + Y);
    let J = !1;
    const _ = () => {
      J || (J = !0, C());
    };
    try {
      const x = await W(F, { signal: b.signal, pause: _ });
      b.signal.aborted || v === E && X === S && (w.value = null, i.value = { status: "synced", value: x });
    } catch (x) {
      b.signal.aborted || (v === E && (w.value = ae(x)), a.pauseOnError && C());
    } finally {
      const x = j(), $ = Date.now();
      m = Math.max(m, $ + x), A.delete(v), h.value = Math.max(0, h.value - 1), V();
      const N = I.get(v);
      N && I.delete(v), t.value && !c && N !== "args" && D();
    }
  }, P = (e = "external") => {
    !t.value || c || !u() || D(0, e);
  }, U = () => {
    if (S += 1, G(), T("args"), !t.value || !u())
      return;
    const e = () => {
      P(
        "external"
        /* External */
      );
    };
    a.debounce > 0 ? (R(), d = setTimeout(() => {
      d = null, e();
    }, a.debounce)) : e();
  }, C = () => {
    t.value && (t.value = !1, k(), R(), T("pause"), f = Date.now(), m = Date.now());
  }, B = () => {
    if (!u() || t.value || !y) return;
    t.value = !0, w.value = null;
    const e = Date.now();
    f = e, m = e, a.triggerOnResume ? P(
      "external"
      /* External */
    ) : D(
      p(),
      "external"
      /* External */
    );
  };
  return te(() => {
    c = !0, k(), R(), T("dispose"), t.value = !1, g.splice(0, g.length).forEach(({ resolve: e }) => e());
  }), ne(
    l,
    (e) => {
      const n = !y;
      z(e), !n && U();
    },
    { flush: "sync", immediate: !0 }
  ), a.autoStart && u() && B(), {
    data: M(i),
    lastError: M(w),
    isActive: M(t),
    inFlightCount: M(h),
    pause: C,
    resume: B
  };
}
export {
  re as usePollingQuery
};
//# sourceMappingURL=usePollingQuery.js.map
