import {
  __toModule
} from "./chunk.2FVDDNKC.js";
import {
  require_react
} from "./chunk.3522QWJU.js";
import {
  __commonJS
} from "./chunk.2VCUNPV2.js";

// ../node_modules/fast-deep-equal/index.js
var require_fast_deep_equal = __commonJS((exports, module) => {
  "use strict";
  module.exports = function equal(a, b) {
    if (a === b)
      return true;
    if (a && b && typeof a == "object" && typeof b == "object") {
      if (a.constructor !== b.constructor)
        return false;
      var length, i, keys;
      if (Array.isArray(a)) {
        length = a.length;
        if (length != b.length)
          return false;
        for (i = length; i-- !== 0; )
          if (!equal(a[i], b[i]))
            return false;
        return true;
      }
      if (a.constructor === RegExp)
        return a.source === b.source && a.flags === b.flags;
      if (a.valueOf !== Object.prototype.valueOf)
        return a.valueOf() === b.valueOf();
      if (a.toString !== Object.prototype.toString)
        return a.toString() === b.toString();
      keys = Object.keys(a);
      length = keys.length;
      if (length !== Object.keys(b).length)
        return false;
      for (i = length; i-- !== 0; )
        if (!Object.prototype.hasOwnProperty.call(b, keys[i]))
          return false;
      for (i = length; i-- !== 0; ) {
        var key = keys[i];
        if (!equal(a[key], b[key]))
          return false;
      }
      return true;
    }
    return a !== a && b !== b;
  };
});

// ../node_modules/use-asset/dist/index.js
var import_react = __toModule(require_react());
var import_fast_deep_equal = __toModule(require_fast_deep_equal());
var globalCache = [];
function handleAsset(fn, cache, args, lifespan = 0, preload = false) {
  for (const entry2 of cache) {
    if ((0, import_fast_deep_equal.default)(args, entry2.args)) {
      if (preload)
        return;
      if (entry2.error)
        throw entry2.error;
      if (entry2.response)
        return entry2.response;
      throw entry2.promise;
    }
  }
  const entry = {
    args,
    promise: fn(...args).then((response) => entry.response = response != null ? response : true).catch((e) => entry.error = e != null ? e : "unknown error").then(() => {
      if (lifespan > 0) {
        setTimeout(() => {
          const index = cache.indexOf(entry);
          if (index !== -1)
            cache.splice(index, 1);
        }, lifespan);
      }
    })
  };
  cache.push(entry);
  if (!preload)
    throw entry.promise;
}
function clear(cache, ...args) {
  if (args === void 0)
    cache.splice(0, cache.length);
  else {
    const entry = cache.find((entry2) => (0, import_fast_deep_equal.default)(args, entry2.args));
    if (entry) {
      const index = cache.indexOf(entry);
      if (index !== -1)
        cache.splice(index, 1);
    }
  }
}
function createAsset(fn, lifespan = 0) {
  const cache = [];
  return {
    read: (...args) => handleAsset(fn, cache, args, lifespan),
    preload: (...args) => void handleAsset(fn, cache, args, lifespan, true),
    clear: (...args) => clear(cache, ...args),
    peek: (...args) => {
      var _cache$find;
      return (_cache$find = cache.find((entry) => (0, import_fast_deep_equal.default)(args, entry.args))) == null ? void 0 : _cache$find.response;
    }
  };
}
function useAsset(fn, ...args) {
  return (0, import_react.useMemo)(() => handleAsset(fn, globalCache, args, useAsset.lifespan), args);
}
useAsset.lifespan = 0;
useAsset.clear = (...args) => clear(globalCache, ...args);
useAsset.preload = (fn, ...args) => void handleAsset(fn, globalCache, args, useAsset.lifespan, true);
useAsset.peek = (...args) => {
  var _globalCache$find;
  return (_globalCache$find = globalCache.find((entry) => (0, import_fast_deep_equal.default)(args, entry.args))) == null ? void 0 : _globalCache$find.response;
};
export {
  createAsset,
  useAsset
};
//# sourceMappingURL=use-asset.js.map
