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/list/hooks/useWaterfall.ts
var useWaterfall_exports = {};
__export(useWaterfall_exports, {
  default: () => useWaterfall_default,
  useWaterfall: () => useWaterfall
});
module.exports = __toCommonJS(useWaterfall_exports);
var import_react = require("react");
var useWaterfall = (items, options) => {
  const [recalcVersion, setRecalcVersion] = (0, import_react.useState)(0);
  const itemSizes = (0, import_react.useRef)(/* @__PURE__ */ new Map());
  const itemRefs = (0, import_react.useRef)(/* @__PURE__ */ new Map());
  const registerItemRef = (0, import_react.useCallback)((id, element) => {
    if (element) {
      itemRefs.current.set(id, element);
    } else {
      itemRefs.current.delete(id);
    }
  }, []);
  const measureItemSize = (0, import_react.useCallback)((id) => {
    const element = itemRefs.current.get(id);
    if (element) {
      const rect = element.getBoundingClientRect();
      const size = { width: rect.width, height: rect.height };
      itemSizes.current.set(id, size);
      return size;
    }
    return itemSizes.current.get(id) || { width: 200, height: 200 };
  }, []);
  const getItemSize = (0, import_react.useCallback)((item) => {
    const { direction } = options;
    if (direction === "vertical") {
      if (item.height) return item.height;
      const measured = itemSizes.current.get(item.id);
      return (measured == null ? void 0 : measured.height) || 200;
    } else {
      if (item.width) return item.width;
      const measured = itemSizes.current.get(item.id);
      return (measured == null ? void 0 : measured.width) || 200;
    }
  }, [options]);
  const layout = (0, import_react.useMemo)(() => {
    const { tracks: trackCount, trackGap, itemGap, direction } = options;
    const newTracks = Array.from({ length: trackCount }, () => ({
      items: [],
      size: 0
    }));
    items.forEach((item) => {
      const shortestTrackIndex = newTracks.reduce((minIndex, track, index, arr) => {
        return track.size < arr[minIndex].size ? index : minIndex;
      }, 0);
      const targetTrack = newTracks[shortestTrackIndex];
      const itemSize = getItemSize(item);
      targetTrack.items.push(item);
      if (targetTrack.items.length > 1) {
        targetTrack.size += itemGap;
      }
      targetTrack.size += itemSize;
    });
    const totalSize = Math.max(...newTracks.map((track) => track.size), 0);
    return {
      tracks: newTracks,
      totalSize
    };
  }, [items, options, recalcVersion, getItemSize]);
  const recalculateLayout = (0, import_react.useCallback)(() => {
    let hasChanges = false;
    itemRefs.current.forEach((element, id) => {
      const rect = element.getBoundingClientRect();
      const newSize = { width: rect.width, height: rect.height };
      const oldSize = itemSizes.current.get(id);
      if (!oldSize || oldSize.width !== newSize.width || oldSize.height !== newSize.height) {
        hasChanges = true;
        itemSizes.current.set(id, newSize);
      }
    });
    if (hasChanges) {
      setRecalcVersion((prev) => prev + 1);
    }
  }, []);
  (0, import_react.useEffect)(() => {
    let timeoutId;
    const handleResize = () => {
      clearTimeout(timeoutId);
      timeoutId = setTimeout(() => {
        recalculateLayout();
      }, 100);
    };
    window.addEventListener("resize", handleResize);
    return () => {
      window.removeEventListener("resize", handleResize);
      clearTimeout(timeoutId);
    };
  }, [recalculateLayout]);
  return {
    layout,
    registerItemRef,
    measureItemSize,
    recalculateLayout
  };
};
var useWaterfall_default = useWaterfall;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  useWaterfall
});
