var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/PisellCards/components/PisellImageCard/hooks/useImageLoad.ts
var useImageLoad_exports = {};
__export(useImageLoad_exports, {
  useImageLoad: () => useImageLoad
});
module.exports = __toCommonJS(useImageLoad_exports);
var import_react = require("react");
var useImageLoad = (src, lazy) => {
  const [loading, setLoading] = (0, import_react.useState)(!!src);
  const [error, setError] = (0, import_react.useState)(false);
  const imgRef = (0, import_react.useRef)(null);
  const observerRef = (0, import_react.useRef)(null);
  (0, import_react.useEffect)(() => {
    if (!src) {
      setLoading(false);
      setError(false);
      return;
    }
    setLoading(true);
    setError(false);
    if (lazy && "IntersectionObserver" in window && imgRef.current) {
      observerRef.current = new IntersectionObserver(
        (entries) => {
          entries.forEach((entry) => {
            var _a;
            if (entry.isIntersecting) {
              loadImage();
              (_a = observerRef.current) == null ? void 0 : _a.disconnect();
            }
          });
        },
        {
          rootMargin: "50px"
          // 提前 50px 开始加载
        }
      );
      observerRef.current.observe(imgRef.current);
      return () => {
        var _a;
        (_a = observerRef.current) == null ? void 0 : _a.disconnect();
      };
    } else {
      loadImage();
    }
  }, [src, lazy]);
  const loadImage = () => {
    if (!src) return;
    const img = new Image();
    img.src = src;
    img.onload = () => {
      setLoading(false);
      setError(false);
    };
    img.onerror = () => {
      setLoading(false);
      setError(true);
    };
  };
  return {
    loading,
    error,
    imgRef
  };
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useImageLoad
});
