import { useState as b, useCallback as u, useMemo as v, useEffect as M } from "react";
import { MiniError as P, formatError as V } from "../utils/errors.js";
const A = (c, d, f) => {
  const [n, l] = b({
    data: null,
    loading: !0,
    error: null
  }), h = f?.skip === !0, { validator: g, hook: t } = f, w = u(
    (s) => {
      try {
        return g?.(s), null;
      } catch (e) {
        return e instanceof Error ? e : new P({
          hook: t,
          message: "Validation failed"
        });
      }
    },
    [g, t]
  ), y = v(() => d, [JSON.stringify(d)]), a = u(
    async (s, {
      setLoading: e = !0,
      setError: p = !0,
      resetOnError: E = !0,
      throwOnError: O = !0
    } = {}) => {
      let m = null, i = null;
      l((r) => ({
        ...r,
        loading: e ? !0 : r.loading
      }));
      try {
        const r = await c({ ...y, ...s });
        if (r.ok)
          i = w(r.data.data), l((S) => ({
            ...S,
            data: r.data.data,
            loading: !1,
            error: i ?? null
          }));
        else
          throw r.error;
      } catch (r) {
        m = V({ hook: t }, r);
      }
      const o = i || m;
      if (o && (p || E) && l((r) => ({
        data: E ? null : r.data,
        loading: !1,
        error: o
      })), o && O)
        throw o;
    },
    [c, y, t, w]
  ), k = u(async () => {
    await a({ fetchPolicy: "network-only" }, {
      setLoading: !1,
      resetOnError: !1,
      throwOnError: !0
    });
  }, [a]);
  return M(() => {
    h || a(
      {},
      {
        throwOnError: !1
      }
    );
  }, [a, h]), {
    data: n.data,
    loading: n.loading,
    error: n.error,
    refetch: k
  };
};
export {
  A as useShopActionsDataFetching
};
//# sourceMappingURL=useShopActionsDataFetching.js.map
