var E = Object.defineProperty;
var f = (h, t, e) => t in h ? E(h, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : h[t] = e;
var n = (h, t, e) => f(h, typeof t != "symbol" ? t + "" : t, e);
import { CACHE_NAME as d, STATUS as a, CACHE_MAX_RETRIES as S } from "./index-config.js";
import { request as y, sleep as C, canUseDOM as D } from "./index-helpers.js";
class u {
  constructor() {
    n(this, "cacheApi");
    n(this, "cacheStore");
    n(this, "subscribers", []);
    n(this, "isReady", !1);
    this.cacheStore = /* @__PURE__ */ new Map();
    let t = d, e = !1;
    D() && (t = window.REACT_INLINESVG_CACHE_NAME ?? d, e = !!window.REACT_INLINESVG_PERSISTENT_CACHE && "caches" in window), e ? caches.open(t).then((s) => {
      this.cacheApi = s, this.isReady = !0, this.subscribers.forEach((c) => c());
    }).catch((s) => {
      this.isReady = !0;
    }) : this.isReady = !0;
  }
  onReady(t) {
    this.isReady ? t() : this.subscribers.push(t);
  }
  async get(t, e) {
    var s;
    return await (this.cacheApi ? this.fetchAndAddToPersistentCache(t, e) : this.fetchAndAddToInternalCache(t, e)), ((s = this.cacheStore.get(t)) == null ? void 0 : s.content) ?? "";
  }
  set(t, e) {
    this.cacheStore.set(t, e);
  }
  isCached(t) {
    var e;
    return ((e = this.cacheStore.get(t)) == null ? void 0 : e.status) === a.LOADED;
  }
  async fetchAndAddToInternalCache(t, e) {
    const s = this.cacheStore.get(t);
    if ((s == null ? void 0 : s.status) === a.LOADING) {
      await this.handleLoading(t, async () => {
        this.cacheStore.set(t, {
          content: "",
          status: a.IDLE
        }), await this.fetchAndAddToInternalCache(t, e);
      });
      return;
    }
    if (!(s != null && s.content)) {
      this.cacheStore.set(t, {
        content: "",
        status: a.LOADING
      });
      try {
        const c = await y(t, e);
        this.cacheStore.set(t, {
          content: c,
          status: a.LOADED
        });
      } catch (c) {
        throw this.cacheStore.set(t, {
          content: "",
          status: a.FAILED
        }), c;
      }
    }
  }
  async fetchAndAddToPersistentCache(t, e) {
    var o, r, A;
    const s = this.cacheStore.get(t);
    if ((s == null ? void 0 : s.status) === a.LOADED)
      return;
    if ((s == null ? void 0 : s.status) === a.LOADING) {
      await this.handleLoading(t, async () => {
        this.cacheStore.set(t, {
          content: "",
          status: a.IDLE
        }), await this.fetchAndAddToPersistentCache(t, e);
      });
      return;
    }
    this.cacheStore.set(t, {
      content: "",
      status: a.LOADING
    });
    const c = await ((o = this.cacheApi) == null ? void 0 : o.match(t));
    if (c) {
      const i = await c.text();
      this.cacheStore.set(t, {
        content: i,
        status: a.LOADED
      });
      return;
    }
    try {
      await ((r = this.cacheApi) == null ? void 0 : r.add(new Request(t, e)));
      const i = await ((A = this.cacheApi) == null ? void 0 : A.match(t)), w = await (i == null ? void 0 : i.text()) ?? "";
      this.cacheStore.set(t, {
        content: w,
        status: a.LOADED
      });
    } catch (i) {
      throw this.cacheStore.set(t, {
        content: "",
        status: a.FAILED
      }), i;
    }
  }
  async handleLoading(t, e) {
    var c;
    let s = 0;
    for (; ((c = this.cacheStore.get(t)) == null ? void 0 : c.status) === a.LOADING && s < S; )
      await C(0.1), s += 1;
    s >= S && await e();
  }
  keys() {
    return [...this.cacheStore.keys()];
  }
  data() {
    return [...this.cacheStore.entries()].map(([t, e]) => ({
      [t]: e
    }));
  }
  async delete(t) {
    this.cacheApi && await this.cacheApi.delete(t), this.cacheStore.delete(t);
  }
  async clear() {
    if (this.cacheApi) {
      const t = await this.cacheApi.keys();
      for (const e of t)
        await this.cacheApi.delete(e);
    }
    this.cacheStore.clear();
  }
}
export {
  u as default
};
