import { timeoutManager as w } from "./timeoutManager.js";
var P = typeof window > "u" || "Deno" in globalThis;
function K() {
}
function E(e, t) {
  return typeof e == "function" ? e(t) : e;
}
function I(e) {
  return typeof e == "number" && e >= 0 && e !== 1 / 0;
}
function T(e, t) {
  return Math.max(e + (t || 0) - Date.now(), 0);
}
function v(e, t) {
  return typeof e == "function" ? e(t) : e;
}
function A(e, t) {
  return typeof e == "function" ? e(t) : e;
}
function N(e, t) {
  const {
    type: r = "all",
    exact: n,
    fetchStatus: i,
    predicate: u,
    queryKey: s,
    stale: f
  } = e;
  if (s) {
    if (n) {
      if (t.queryHash !== k(s, t.options))
        return !1;
    } else if (!O(t.queryKey, s))
      return !1;
  }
  if (r !== "all") {
    const o = t.isActive();
    if (r === "active" && !o || r === "inactive" && o)
      return !1;
  }
  return !(typeof f == "boolean" && t.isStale() !== f || i && i !== t.state.fetchStatus || u && !u(t));
}
function H(e, t) {
  const { exact: r, status: n, predicate: i, mutationKey: u } = e;
  if (u) {
    if (!t.options.mutationKey)
      return !1;
    if (r) {
      if (p(t.options.mutationKey) !== p(u))
        return !1;
    } else if (!O(t.options.mutationKey, u))
      return !1;
  }
  return !(n && t.state.status !== n || i && !i(t));
}
function k(e, t) {
  return (t?.queryKeyHashFn || p)(e);
}
function p(e) {
  return JSON.stringify(
    e,
    (t, r) => g(r) ? Object.keys(r).sort().reduce((n, i) => (n[i] = r[i], n), {}) : r
  );
}
function O(e, t) {
  return e === t ? !0 : typeof e != typeof t ? !1 : e && t && typeof e == "object" && typeof t == "object" ? Object.keys(t).every((r) => O(e[r], t[r])) : !1;
}
var q = Object.prototype.hasOwnProperty;
function m(e, t, r = 0) {
  if (e === t)
    return e;
  if (r > 500) return t;
  const n = S(e) && S(t);
  if (!n && !(g(e) && g(t))) return t;
  const u = (n ? e : Object.keys(e)).length, s = n ? t : Object.keys(t), f = s.length, o = n ? new Array(f) : {};
  let h = 0;
  for (let l = 0; l < f; l++) {
    const c = n ? l : s[l], a = e[c], y = t[c];
    if (a === y) {
      o[c] = a, (n ? l < u : q.call(e, c)) && h++;
      continue;
    }
    if (a === null || y === null || typeof a != "object" || typeof y != "object") {
      o[c] = y;
      continue;
    }
    const j = m(a, y, r + 1);
    o[c] = j, j === a && h++;
  }
  return u === f && h === u ? e : o;
}
function M(e, t) {
  if (!t || Object.keys(e).length !== Object.keys(t).length)
    return !1;
  for (const r in e)
    if (e[r] !== t[r])
      return !1;
  return !0;
}
function S(e) {
  return Array.isArray(e) && e.length === Object.keys(e).length;
}
function g(e) {
  if (!d(e))
    return !1;
  const t = e.constructor;
  if (t === void 0)
    return !0;
  const r = t.prototype;
  return !(!d(r) || !r.hasOwnProperty("isPrototypeOf") || Object.getPrototypeOf(e) !== Object.prototype);
}
function d(e) {
  return Object.prototype.toString.call(e) === "[object Object]";
}
function z(e) {
  return new Promise((t) => {
    w.setTimeout(t, e);
  });
}
function D(e, t, r) {
  if (typeof r.structuralSharing == "function")
    return r.structuralSharing(e, t);
  if (r.structuralSharing !== !1) {
    if (process.env.NODE_ENV !== "production")
      try {
        return m(e, t);
      } catch (n) {
        throw console.error(
          `Structural sharing requires data to be JSON serializable. To fix this, turn off structuralSharing or return JSON-serializable data from your queryFn. [${r.queryHash}]: ${n}`
        ), n;
      }
    return m(e, t);
  }
  return t;
}
function Q(e, t, r = 0) {
  const n = [...e, t];
  return r && n.length > r ? n.slice(1) : n;
}
function $(e, t, r = 0) {
  const n = [t, ...e];
  return r && n.length > r ? n.slice(0, -1) : n;
}
var b = /* @__PURE__ */ Symbol();
function x(e, t) {
  return process.env.NODE_ENV !== "production" && e.queryFn === b && console.error(
    `Attempted to invoke queryFn when set to skipToken. This is likely a configuration error. Query hash: '${e.queryHash}'`
  ), !e.queryFn && t?.initialPromise ? () => t.initialPromise : !e.queryFn || e.queryFn === b ? () => Promise.reject(new Error(`Missing queryFn: '${e.queryHash}'`)) : e.queryFn;
}
function J(e, t) {
  return typeof e == "function" ? e(...t) : !!e;
}
function V(e, t, r) {
  let n = !1, i;
  return Object.defineProperty(e, "signal", {
    enumerable: !0,
    get: () => (i ??= t(), n || (n = !0, i.aborted ? r() : i.addEventListener("abort", r, { once: !0 })), i)
  }), e;
}
export {
  V as addConsumeAwareSignal,
  Q as addToEnd,
  $ as addToStart,
  x as ensureQueryFn,
  E as functionalUpdate,
  p as hashKey,
  k as hashQueryKeyByOptions,
  S as isPlainArray,
  g as isPlainObject,
  P as isServer,
  I as isValidTimeout,
  H as matchMutation,
  N as matchQuery,
  K as noop,
  O as partialMatchKey,
  D as replaceData,
  m as replaceEqualDeep,
  A as resolveEnabled,
  v as resolveStaleTime,
  M as shallowEqualObjects,
  J as shouldThrowError,
  b as skipToken,
  z as sleep,
  T as timeUntilStale
};
