"use client";

// src/lib/lazy-loading.ts
import * as React from "react";
var DEFAULT_THRESHOLD = 300;
var DEFAULT_CHUNK_SIZE = 200;
var DEFAULT_INTERVAL_MS = 48;
function useIncrementalRender(totalCount, {
  enabled = true,
  threshold = DEFAULT_THRESHOLD,
  chunkSize = DEFAULT_CHUNK_SIZE,
  intervalMs = DEFAULT_INTERVAL_MS
} = {}) {
  const safeTotal = Math.max(0, totalCount);
  const shouldIncrementalRender = enabled && safeTotal > threshold;
  const initialCount = shouldIncrementalRender ? Math.min(safeTotal, chunkSize) : safeTotal;
  const [renderCount, setRenderCount] = React.useState(initialCount);
  React.useEffect(() => {
    setRenderCount(initialCount);
  }, [initialCount, safeTotal]);
  React.useEffect(() => {
    if (!shouldIncrementalRender) return;
    if (renderCount >= safeTotal) return;
    let cancelled = false;
    const flush = () => {
      if (cancelled) return;
      setRenderCount((prev) => Math.min(safeTotal, prev + chunkSize));
    };
    if (typeof globalThis !== "undefined" && "requestIdleCallback" in globalThis && typeof globalThis.requestIdleCallback === "function") {
      const idleId = globalThis.requestIdleCallback(flush, { timeout: intervalMs });
      return () => {
        cancelled = true;
        globalThis.cancelIdleCallback(idleId);
      };
    }
    const timer = globalThis.setTimeout(flush, intervalMs);
    return () => {
      cancelled = true;
      globalThis.clearTimeout(timer);
    };
  }, [chunkSize, intervalMs, renderCount, safeTotal, shouldIncrementalRender]);
  return {
    renderCount,
    isLazyLoading: shouldIncrementalRender && renderCount < safeTotal,
    shouldIncrementalRender
  };
}
export {
  useIncrementalRender
};
//# sourceMappingURL=lazy-loading.js.map