import { useState as m, useEffect as p } from "react";
import { SVG_ICON_ERROR as S, SVG_CACHE_PREFIX as l, REPLACEMENT_PRYMARY_COLOR as u, REPLACEMENT_SECONDARY_COLOR as v } from "./constants.js";
import { getSVG as $, purgedColor as g, setWindowSvgCache as h, getWindowSvgCache as y } from "./helpers.js";
function I(E) {
  const {
    src: r,
    cacheKey: e,
    primaryColor: t,
    secondaryColor: c = "#FFFFFF",
    skeletonIcon: R,
    fallBackIcon: n = S
  } = E, [a, d] = m($(r, e, t, R));
  return p(() => {
    if (!r)
      return;
    (async () => {
      if (a && a.src === r && a.cacheKey === e && a.primaryColor === t && a.secondaryColor === c)
        return;
      let s, i = !1;
      const f = `${l}_${e}_primary_${g(t)}_secondary_${g(c)}`, C = e ? y(f) : void 0;
      if (C)
        s = C;
      else {
        let o;
        if (e && (o = y(`${l}_${e}_raw`)), !o) {
          try {
            o = await fetch(r).then((_) => _.text()), e && typeof o == "string" && h(`${l}_${e}_raw`, o);
          } catch {
          }
          o || (i = !0, o = n);
        }
        s = "data:image/svg+xml;base64," + btoa(
          (o || "").replace(u, t).replace(v, c)
        ), e && !i && h(f, s);
      }
      d({
        src: r,
        primaryColor: t,
        secondaryColor: c,
        cacheKey: e,
        dataWithColor: s,
        isSkeleton: !1,
        isError: i
      });
    })();
  }, [r, t, e, c, n]), a;
}
export {
  I as useSvgColor
};
