import { getCurrentInstance as q, inject as M, unref as k, watch as S, isRef as L, shallowRef as _, ref as C } from "vue";
const D = /* @__PURE__ */ new WeakMap(), U = (...e) => {
  var r;
  const o = e[0], t = (r = q()) == null ? void 0 : r.proxy;
  if (t == null)
    throw new Error("injectLocal must be called in setup");
  return D.has(t) && o in D.get(t) ? D.get(t)[o] : M(...e);
};
function g(e) {
  return typeof e == "function" ? e() : k(e);
}
typeof WorkerGlobalScope < "u" && globalThis instanceof WorkerGlobalScope;
const B = () => {
};
function N(e, r = !1, o = "Timeout") {
  return new Promise((t, s) => {
    setTimeout(r ? () => s(o) : t, e);
  });
}
function A(e, r = !1) {
  function o(n, { flush: u = "sync", deep: c = !1, timeout: f, throwOnTimeout: P } = {}) {
    let d = null;
    const y = [new Promise((v) => {
      d = S(
        e,
        (m) => {
          n(m) !== r && (d == null || d(), v(m));
        },
        {
          flush: u,
          deep: c,
          immediate: !0
        }
      );
    })];
    return f != null && y.push(
      N(f, P).then(() => g(e)).finally(() => d == null ? void 0 : d())
    ), Promise.race(y);
  }
  function t(n, u) {
    if (!L(n))
      return o((m) => m === n, u);
    const { flush: c = "sync", deep: f = !1, timeout: P, throwOnTimeout: d } = u ?? {};
    let i = null;
    const v = [new Promise((m) => {
      i = S(
        [e, n],
        ([a, T]) => {
          r !== (a === T) && (i == null || i(), m(a));
        },
        {
          flush: c,
          deep: f,
          immediate: !0
        }
      );
    })];
    return P != null && v.push(
      N(P, d).then(() => g(e)).finally(() => (i == null || i(), g(e)))
    ), Promise.race(v);
  }
  function s(n) {
    return o((u) => !!u, n);
  }
  function l(n) {
    return t(null, n);
  }
  function x(n) {
    return t(void 0, n);
  }
  function w(n) {
    return o(Number.isNaN, n);
  }
  function b(n, u) {
    return o((c) => {
      const f = Array.from(c);
      return f.includes(n) || f.includes(g(n));
    }, u);
  }
  function O(n) {
    return h(1, n);
  }
  function h(n = 1, u) {
    let c = -1;
    return o(() => (c += 1, c >= n), u);
  }
  return Array.isArray(g(e)) ? {
    toMatch: o,
    toContains: b,
    changed: O,
    changedTimes: h,
    get not() {
      return A(e, !r);
    }
  } : {
    toMatch: o,
    toBe: t,
    toBeTruthy: s,
    toBeNull: l,
    toBeNaN: w,
    toBeUndefined: x,
    changed: O,
    changedTimes: h,
    get not() {
      return A(e, !r);
    }
  };
}
function X(e) {
  return A(e);
}
const j = Symbol("RESOURCE_CONTEXT"), G = () => U(j, { name: "" }), I = {
  create: () => Promise.resolve({ data: null }),
  // avoids adding a context in tests
  deleteOne: () => Promise.resolve({ data: null }),
  // avoids adding a context in tests
  getList: () => Promise.resolve({
    data: Array.from({ length: 30 }).map((e, r) => ({
      name: r,
      id: r
    })),
    total: 20
  }),
  // avoids adding a context in tests
  getMany: () => Promise.resolve({ data: [] }),
  // avoids adding a context in tests
  getManyReference: () => Promise.resolve({ data: [], total: 0 }),
  // avoids adding a context in tests
  getOne: () => Promise.resolve({ data: null }),
  // avoids adding a context in tests
  update: () => Promise.resolve({ data: null })
  // avoids adding a context in tests
}, V = Symbol("DATA_PROVIDER_CONTEXT"), E = () => U(V, I);
function R(e) {
  const {
    initialData: r,
    shallow: o,
    onSuccess: t = B,
    onError: s = B,
    immediate: l,
    resetOnExecute: x = !1,
    queryFn: w,
    queryKey: b
  } = e, O = (o ? _ : C)(r), h = C(!1), n = C(!1), u = C(!1), c = _(), f = (a) => {
    n.value = a, h.value = !a;
  }, P = () => {
    x && (O.value = r);
  }, d = () => new Promise((a, T) => {
    X(h).toBe(!0).then(() => c.value ? T(c.value) : a(m));
  }), i = {
    then: (...a) => d().then(...a),
    catch: (...a) => d().catch(...a)
  };
  let y = 0;
  const v = (a) => {
    if (c.value = void 0, n.value)
      return i;
    if (w === void 0)
      return h.value = !0, i;
    P(), f(!0), y += 1;
    const T = y;
    return w(a).then((p) => {
      const F = p;
      O.value = F, t(F);
    }).catch((p) => {
      c.value = p, s(p);
    }).finally(() => {
      var p;
      (p = e.onFinish) == null || p.call(e), T === y && f(!1);
    }), i;
  };
  l && v();
  const m = {
    data: O,
    error: c,
    isFinished: h,
    isLoading: n,
    isAborted: u,
    isCanceled: u,
    execute: v
  };
  return {
    ...m,
    ...i
  };
}
const W = (e, r = !1) => {
  const { create: o } = E(), t = R({
    immediate: r,
    queryFn: (l) => o(e, l)
  }), s = t.execute;
  return { ...t, sendCreate: s };
}, H = (e, r = !1) => {
  const { deleteOne: o } = E(), t = R({
    immediate: r,
    queryFn: (l) => o(e, l)
  }), s = t.execute;
  return { ...t, sendDelete: s };
}, K = (e, r = !1) => {
  const { getOne: o } = E(), t = R({
    immediate: r,
    queryFn: (l) => o(e, l)
  }), s = t.execute;
  return { ...t, getOneData: s };
}, z = (e, r = !1) => {
  const { update: o } = E(), t = R({
    immediate: r,
    queryFn: (l) => o(e, l)
  }), s = t.execute;
  return { ...t, sendUpdate: s };
}, Q = () => {
  const { name: e } = G(), r = z(e), o = H(e), t = W(e), s = K(e);
  return {
    update: r,
    deleteOne: o,
    create: t,
    getOne: s
  };
};
export {
  Q as useCurd
};
//# sourceMappingURL=useCurd.mjs.map
