import * as m from "react";
import O from "./index-esm4.js";
import { STATUS as p, randomString as E, canUseDOM as w, isSupportedEnvironment as S, omit as P } from "./index-helpers3.js";
var A = /* @__PURE__ */ (function() {
  var l = function(a, t) {
    return l = Object.setPrototypeOf || { __proto__: [] } instanceof Array && function(e, r) {
      e.__proto__ = r;
    } || function(e, r) {
      for (var n in r) Object.prototype.hasOwnProperty.call(r, n) && (e[n] = r[n]);
    }, l(a, t);
  };
  return function(a, t) {
    if (typeof t != "function" && t !== null)
      throw new TypeError("Class extends value " + String(t) + " is not a constructor or null");
    l(a, t);
    function e() {
      this.constructor = a;
    }
    a.prototype = t === null ? Object.create(t) : (e.prototype = t.prototype, new e());
  };
})(), _ = function() {
  return _ = Object.assign || function(l) {
    for (var a, t = 1, e = arguments.length; t < e; t++) {
      a = arguments[t];
      for (var r in a) Object.prototype.hasOwnProperty.call(a, r) && (l[r] = a[r]);
    }
    return l;
  }, _.apply(this, arguments);
}, g = function(l, a) {
  var t = typeof Symbol == "function" && l[Symbol.iterator];
  if (!t) return l;
  var e = t.call(l), r, n = [], u;
  try {
    for (; (a === void 0 || a-- > 0) && !(r = e.next()).done; ) n.push(r.value);
  } catch (i) {
    u = { error: i };
  } finally {
    try {
      r && !r.done && (t = e.return) && t.call(e);
    } finally {
      if (u) throw u.error;
    }
  }
  return n;
}, D = function(l, a, t) {
  if (t || arguments.length === 2) for (var e = 0, r = a.length, n; e < r; e++)
    (n || !(e in a)) && (n || (n = Array.prototype.slice.call(a, 0, e)), n[e] = a[e]);
  return l.concat(n || Array.prototype.slice.call(a));
}, h = /* @__PURE__ */ Object.create(null), L = (
  /** @class */
  (function(l) {
    A(a, l);
    function a(t) {
      var e = l.call(this, t) || this;
      return Object.defineProperty(e, "isInitialized", {
        enumerable: !0,
        configurable: !0,
        writable: !0,
        value: !1
      }), Object.defineProperty(e, "isActive", {
        enumerable: !0,
        configurable: !0,
        writable: !0,
        value: !1
      }), Object.defineProperty(e, "hash", {
        enumerable: !0,
        configurable: !0,
        writable: !0,
        value: void 0
      }), Object.defineProperty(e, "handleLoad", {
        enumerable: !0,
        configurable: !0,
        writable: !0,
        value: function(r, n) {
          n === void 0 && (n = !1), e.isActive && e.setState({
            content: r,
            hasCache: n,
            status: p.LOADED
          }, e.getElement);
        }
      }), Object.defineProperty(e, "handleError", {
        enumerable: !0,
        configurable: !0,
        writable: !0,
        value: function(r) {
          var n = e.props.onError, u = r.message === "Browser does not support SVG" ? p.UNSUPPORTED : p.FAILED;
          e.isActive && e.setState({ status: u }, function() {
            typeof n == "function" && n(r);
          });
        }
      }), Object.defineProperty(e, "request", {
        enumerable: !0,
        configurable: !0,
        writable: !0,
        value: function() {
          var r = e.props, n = r.cacheRequests, u = r.fetchOptions, i = r.src;
          try {
            return n && (h[i] = { content: "", status: p.LOADING }), fetch(i, u).then(function(o) {
              var s = o.headers.get("content-type"), f = g((s || "").split(/ ?; ?/), 1), c = f[0];
              if (o.status > 299)
                throw new Error("Not found");
              if (!["image/svg+xml", "text/plain"].some(function(d) {
                return c.includes(d);
              }))
                throw new Error("Content type isn't valid: ".concat(c));
              return o.text();
            }).then(function(o) {
              var s = e.props.src;
              if (i !== s) {
                h[i].status === p.LOADING && delete h[i];
                return;
              }
              if (e.handleLoad(o), n) {
                var f = h[i];
                f && (f.content = o, f.status = p.LOADED);
              }
            }).catch(function(o) {
              if (e.handleError(o), n) {
                var s = h[i];
                s && delete h[i];
              }
            });
          } catch (o) {
            return e.handleError(new Error(o.message));
          }
        }
      }), e.state = {
        content: "",
        element: null,
        hasCache: !!t.cacheRequests && !!h[t.src],
        status: p.PENDING
      }, e.hash = t.uniqueHash || E(8), e;
    }
    return Object.defineProperty(a.prototype, "componentDidMount", {
      enumerable: !1,
      configurable: !0,
      writable: !0,
      value: function() {
        if (this.isActive = !0, !(!w() || this.isInitialized)) {
          var t = this.state.status, e = this.props.src;
          try {
            if (t === p.PENDING) {
              if (!S())
                throw new Error("Browser does not support SVG");
              if (!e)
                throw new Error("Missing src");
              this.load();
            }
          } catch (r) {
            this.handleError(r);
          }
          this.isInitialized = !0;
        }
      }
    }), Object.defineProperty(a.prototype, "componentDidUpdate", {
      enumerable: !1,
      configurable: !0,
      writable: !0,
      value: function(t, e) {
        if (w()) {
          var r = this.state, n = r.hasCache, u = r.status, i = this.props, o = i.onLoad, s = i.src;
          if (e.status !== p.READY && u === p.READY && o && o(s, n), t.src !== s) {
            if (!s) {
              this.handleError(new Error("Missing src"));
              return;
            }
            this.load();
          }
        }
      }
    }), Object.defineProperty(a.prototype, "componentWillUnmount", {
      enumerable: !1,
      configurable: !0,
      writable: !0,
      value: function() {
        this.isActive = !1;
      }
    }), Object.defineProperty(a.prototype, "getNode", {
      enumerable: !1,
      configurable: !0,
      writable: !0,
      value: function() {
        var t = this.props, e = t.description, r = t.title;
        try {
          var n = this.processSVG(), u = O(n, { nodeOnly: !0 });
          if (!u || !(u instanceof SVGSVGElement))
            throw new Error("Could not convert the src to a DOM Node");
          var i = this.updateSVGAttributes(u);
          if (e) {
            var o = i.querySelector("desc");
            o && o.parentNode && o.parentNode.removeChild(o);
            var s = document.createElementNS("http://www.w3.org/2000/svg", "desc");
            s.innerHTML = e, i.prepend(s);
          }
          if (typeof r < "u") {
            var f = i.querySelector("title");
            if (f && f.parentNode && f.parentNode.removeChild(f), r) {
              var c = document.createElementNS("http://www.w3.org/2000/svg", "title");
              c.innerHTML = r, i.prepend(c);
            }
          }
          return i;
        } catch (d) {
          return this.handleError(d);
        }
      }
    }), Object.defineProperty(a.prototype, "getElement", {
      enumerable: !1,
      configurable: !0,
      writable: !0,
      value: function() {
        try {
          var t = this.getNode(), e = O(t);
          if (!e || !m.isValidElement(e))
            throw new Error("Could not convert the src to a React element");
          this.setState({
            element: e,
            status: p.READY
          });
        } catch (r) {
          this.handleError(new Error(r.message));
        }
      }
    }), Object.defineProperty(a.prototype, "load", {
      enumerable: !1,
      configurable: !0,
      writable: !0,
      value: function() {
        var t = this;
        this.isActive && this.setState({
          content: "",
          element: null,
          hasCache: !1,
          status: p.LOADING
        }, function() {
          var e = t.props, r = e.cacheRequests, n = e.src, u = r && h[n];
          if (u && u.status === p.LOADED) {
            t.handleLoad(u.content, !0);
            return;
          }
          var i = n.match(/data:image\/svg[^,]*?(;base64)?,(.*)/), o;
          if (i ? o = i[1] ? window.atob(i[2]) : decodeURIComponent(i[2]) : n.includes("<svg") && (o = n), o) {
            t.handleLoad(o);
            return;
          }
          t.request();
        });
      }
    }), Object.defineProperty(a.prototype, "updateSVGAttributes", {
      enumerable: !1,
      configurable: !0,
      writable: !0,
      value: function(t) {
        var e = this, r = this.props, n = r.baseURL, u = n === void 0 ? "" : n, i = r.uniquifyIDs, o = ["id", "href", "xlink:href", "xlink:role", "xlink:arcrole"], s = ["href", "xlink:href"], f = function(c, d) {
          return s.includes(c) && (d ? !d.includes("#") : !1);
        };
        return i && D([], g(t.children), !1).map(function(c) {
          if (c.attributes && c.attributes.length) {
            var d = Object.values(c.attributes).map(function(b) {
              var v = b, y = b.value.match(/url\((.*?)\)/);
              return y && y[1] && (v.value = b.value.replace(y[0], "url(".concat(u).concat(y[1], "__").concat(e.hash, ")"))), v;
            });
            o.forEach(function(b) {
              var v = d.find(function(y) {
                return y.name === b;
              });
              v && !f(b, v.value) && (v.value = "".concat(v.value, "__").concat(e.hash));
            });
          }
          return c.children.length ? e.updateSVGAttributes(c) : c;
        }), t;
      }
    }), Object.defineProperty(a.prototype, "processSVG", {
      enumerable: !1,
      configurable: !0,
      writable: !0,
      value: function() {
        var t = this.state.content, e = this.props.preProcessor;
        return e ? e(t) : t;
      }
    }), Object.defineProperty(a.prototype, "render", {
      enumerable: !1,
      configurable: !0,
      writable: !0,
      value: function() {
        var t = this.state, e = t.element, r = t.status, n = this.props, u = n.children, i = u === void 0 ? null : u, o = n.innerRef, s = n.loader, f = s === void 0 ? null : s, c = P(this.props, "baseURL", "cacheRequests", "children", "description", "fetchOptions", "innerRef", "loader", "onError", "onLoad", "preProcessor", "src", "title", "uniqueHash", "uniquifyIDs");
        return w() ? e ? m.cloneElement(e, _({ ref: o }, c)) : [p.UNSUPPORTED, p.FAILED].includes(r) ? i : f : f;
      }
    }), Object.defineProperty(a, "defaultProps", {
      enumerable: !0,
      configurable: !0,
      writable: !0,
      value: {
        cacheRequests: !0,
        uniquifyIDs: !1
      }
    }), a;
  })(m.PureComponent)
);
export {
  h as cacheStore,
  L as default
};
