import _defineProperty from "../../../../@babel_runtime@7.22.15/node_modules/@babel/runtime/helpers/esm/defineProperty.js";
import { useState } from "react";
var imagePromiseFactory = function(_ref) {
  var _ref$decode = _ref.decode, decode = _ref$decode === void 0 ? true : _ref$decode, _ref$crossOrigin = _ref.crossOrigin, crossOrigin = _ref$crossOrigin === void 0 ? "" : _ref$crossOrigin;
  return function(src) {
    return new Promise(function(resolve, reject) {
      var i = new Image();
      if (crossOrigin)
        i.crossOrigin = crossOrigin;
      i.onload = function() {
        decode && i.decode ? i.decode().then(resolve)["catch"](reject) : resolve();
      };
      i.onerror = reject;
      i.src = src;
    });
  };
};
function ownKeys(object, enumerableOnly) {
  var keys = Object.keys(object);
  if (Object.getOwnPropertySymbols) {
    var symbols = Object.getOwnPropertySymbols(object);
    enumerableOnly && (symbols = symbols.filter(function(sym) {
      return Object.getOwnPropertyDescriptor(object, sym).enumerable;
    })), keys.push.apply(keys, symbols);
  }
  return keys;
}
function _objectSpread(target) {
  for (var i = 1; i < arguments.length; i++) {
    var source = null != arguments[i] ? arguments[i] : {};
    i % 2 ? ownKeys(Object(source), true).forEach(function(key) {
      _defineProperty(target, key, source[key]);
    }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function(key) {
      Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
    });
  }
  return target;
}
var removeBlankArrayElements = function removeBlankArrayElements2(a) {
  return a.filter(function(x) {
    return x;
  });
};
var stringToArray = function stringToArray2(x) {
  return Array.isArray(x) ? x : [x];
};
var cache = {};
var promiseFind = function promiseFind2(arr, promiseFactory) {
  var done = false;
  return new Promise(function(resolve, reject) {
    var queueNext = function queueNext2(src) {
      return promiseFactory(src).then(function() {
        done = true;
        resolve(src);
      });
    };
    arr.reduce(function(p, src) {
      return p["catch"](function() {
        if (!done)
          return queueNext(src);
      });
    }, queueNext(arr.shift()))["catch"](reject);
  });
};
function useImage(_ref) {
  var srcList = _ref.srcList, _ref$imgPromise = _ref.imgPromise, imgPromise = _ref$imgPromise === void 0 ? imagePromiseFactory({
    decode: true
  }) : _ref$imgPromise, _ref$useSuspense = _ref.useSuspense, useSuspense = _ref$useSuspense === void 0 ? true : _ref$useSuspense;
  var _useState = useState(false), setIsSettled = _useState[1];
  var sourceList = removeBlankArrayElements(stringToArray(srcList));
  var sourceKey = sourceList.join("");
  if (!cache[sourceKey]) {
    cache[sourceKey] = {
      promise: promiseFind(sourceList, imgPromise),
      cache: "pending",
      error: null
    };
  }
  if (cache[sourceKey].cache === "resolved") {
    return {
      src: cache[sourceKey].src,
      isLoading: false,
      error: null
    };
  }
  if (cache[sourceKey].cache === "rejected") {
    if (useSuspense)
      throw cache[sourceKey].error;
    return {
      isLoading: false,
      error: cache[sourceKey].error,
      src: void 0
    };
  }
  cache[sourceKey].promise.then(function(src) {
    cache[sourceKey] = _objectSpread(_objectSpread({}, cache[sourceKey]), {}, {
      cache: "resolved",
      src
    });
    if (!useSuspense)
      setIsSettled(sourceKey);
  })["catch"](function(error) {
    cache[sourceKey] = _objectSpread(_objectSpread({}, cache[sourceKey]), {}, {
      cache: "rejected",
      error
    });
    if (!useSuspense)
      setIsSettled(sourceKey);
  });
  if (useSuspense)
    throw cache[sourceKey].promise;
  return {
    isLoading: true,
    src: void 0,
    error: null
  };
}
export {
  imagePromiseFactory as i,
  useImage as u
};
//# sourceMappingURL=useImage-65024c8f.js.map
