import { jsx as k } from "react/jsx-runtime";
import { useRef as C, useState as B, useEffect as A, useReducer as F, useCallback as a, isValidElement as H, cloneElement as j } from "react";
import z from "./index-esm4.js";
import { STATUS as t } from "./index-config.js";
import J from "./index-cache.js";
import { randomString as K, request as Q, canUseDOM as L, isSupportedEnvironment as W, omit as X } from "./index-helpers.js";
import { usePrevious as V } from "./index-hooks.js";
import { getNode as Z } from "./index-utils2.js";
let u;
function _(s) {
  const {
    cacheRequests: f = !0,
    children: E = null,
    description: D,
    fetchOptions: c,
    innerRef: T,
    loader: O = null,
    onError: R,
    onLoad: w,
    src: r,
    title: v,
    uniqueHash: x
  } = s, [y, S] = F((e, n) => ({
    ...e,
    ...n
  }), {
    content: "",
    element: null,
    isCached: f && u.isCached(s.src),
    status: t.IDLE
  }), {
    content: U,
    element: P,
    isCached: G,
    status: i
  } = y, l = V(s), d = V(y), M = C(x ?? K(8)), m = C(!1), q = C(!1), o = a((e) => {
    m.current && (S({
      status: e.message === "Browser does not support SVG" ? t.UNSUPPORTED : t.FAILED
    }), R == null || R(e));
  }, [R]), h = a((e, n = !1) => {
    m.current && S({
      content: e,
      isCached: n,
      status: t.LOADED
    });
  }, []), N = a(async () => {
    const e = await Q(r, c);
    h(e);
  }, [c, h, r]), p = a(() => {
    try {
      const e = Z({
        ...s,
        handleError: o,
        hash: M.current,
        content: U
      }), n = z(e);
      if (!n || !H(n))
        throw new Error("Could not convert the src to a React element");
      S({
        element: n,
        status: t.READY
      });
    } catch (e) {
      o(new Error(e.message));
    }
  }, [U, o, s]), b = a(async () => {
    const e = /^data:image\/svg[^,]*?(;base64)?,(.*)/u.exec(r);
    let n;
    if (e ? n = e[1] ? window.atob(e[2]) : decodeURIComponent(e[2]) : r.includes("<svg") && (n = r), n) {
      h(n);
      return;
    }
    try {
      if (f) {
        const g = await u.get(r, c);
        h(g, !0);
      } else
        await N();
    } catch (g) {
      o(g);
    }
  }, [f, N, c, o, h, r]), I = a(() => {
    m.current && S({
      content: "",
      element: null,
      isCached: !1,
      status: t.LOADING
    });
  }, []);
  A(
    () => {
      if (m.current = !0, !L() || q.current)
        return () => {
        };
      try {
        if (i === t.IDLE) {
          if (!W())
            throw new Error("Browser does not support SVG");
          if (!r)
            throw new Error("Missing src");
          I();
        }
      } catch (e) {
        o(e);
      }
      return q.current = !0, () => {
        m.current = !1;
      };
    },
    // eslint-disable-next-line react-hooks/exhaustive-deps
    []
  ), A(() => {
    if (L() && l)
      if (l.src !== r) {
        if (!r) {
          o(new Error("Missing src"));
          return;
        }
        I();
      } else (l.title !== v || l.description !== D) && p();
  }, [D, p, o, I, l, r, v]), A(() => {
    d && (d.status !== t.LOADING && i === t.LOADING && b(), d.status !== t.LOADED && i === t.LOADED && p(), d.status !== t.READY && i === t.READY && (w == null || w(r, G)));
  }, [b, p, G, w, d, r, i]);
  const Y = X(s, "baseURL", "cacheRequests", "children", "description", "fetchOptions", "innerRef", "loader", "onError", "onLoad", "preProcessor", "src", "title", "uniqueHash", "uniquifyIDs");
  return L() ? P ? j(P, {
    ref: T,
    ...Y
  }) : [t.UNSUPPORTED, t.FAILED].includes(i) ? E : O : O;
}
function ie(s) {
  u || (u = new J());
  const {
    loader: f
  } = s, E = C(!1), [D, c] = B(u.isReady);
  return A(() => {
    E.current || (u.onReady(() => {
      c(!0);
    }), E.current = !0);
  }, []), D ? k(_, {
    ...s
  }) : f;
}
export {
  u as cacheStore,
  ie as default
};
