import { jsx as c } from "react/jsx-runtime";
import { urlWithQuery as m } from "@argent/x-shared";
import { useContext as u, useMemo as a, createContext as f } from "react";
const d = (t) => {
  try {
    return new URL(t).searchParams.get("url");
  } catch {
  }
}, p = (t, { imageOptimizationUrl: r, width: n = 1024, format: e }) => {
  if (!t)
    return;
  if (!r)
    return t;
  const o = {
    url: t
  }, i = d(t);
  if (i) {
    if (i.startsWith("data"))
      return i;
    o.url = i;
  }
  return n !== void 0 && (o.w = `${n}`), e && (o.format = e), m(r, o);
}, s = f({}), x = () => u(s), h = (t, r) => {
  const { imageOptimizationUrl: n, getOptimizedImageUrl: e } = x();
  return a(() => {
    if (t)
      return t.startsWith("data") ? t : e ? e(t, r) : p(t, {
        ...r,
        imageOptimizationUrl: n
      });
  }, [e, t, r, n]);
}, v = ({ imageOptimizationUrl: t, getOptimizedImageUrl: r, children: n }) => {
  const e = a(() => ({
    imageOptimizationUrl: t,
    getOptimizedImageUrl: r
  }), [t, r]);
  return /* @__PURE__ */ c(s.Provider, { value: e, children: n });
};
export {
  v as ImageOptimizationProvider,
  h as useGetOptimizedImageUrl
};
