"use client";

// src/components/data-views/data-row-list.tsx
import * as React from "react";
import { useWindowVirtualizer } from "@tanstack/react-virtual";

// src/lib/utils.ts
import { clsx } from "clsx";
import { twMerge } from "tailwind-merge";
function cn(...inputs) {
  return twMerge(clsx(inputs));
}

// src/components/data-views/data-row-list.tsx
import { Fragment, jsx } from "react/jsx-runtime";
var DEFAULT_VIRTUALIZE_THRESHOLD = 100;
var DEFAULT_ESTIMATED_ROW_HEIGHT = 96;
var DEFAULT_OVERSCAN = 8;
var DEFAULT_OUTER_CLASS = "flex list-none flex-col gap-2 px-4 pb-8 pt-2 lg:px-6";
function DataRowListItem({
  row,
  index,
  renderRow
}) {
  return /* @__PURE__ */ jsx(Fragment, { children: renderRow(row, index) });
}
function DataRowList(props) {
  const {
    rows,
    getRowId,
    renderRow,
    emptyState,
    virtualizeThreshold = DEFAULT_VIRTUALIZE_THRESHOLD,
    estimatedRowHeight = DEFAULT_ESTIMATED_ROW_HEIGHT,
    className,
    rowClassName,
    ariaLabel
  } = props;
  if (rows.length === 0) {
    if (emptyState == null) return null;
    if (typeof emptyState === "string") {
      return /* @__PURE__ */ jsx("div", { className: "px-4 py-16 text-center lg:px-6", children: /* @__PURE__ */ jsx("p", { className: "text-sm text-muted-foreground", children: emptyState }) });
    }
    return /* @__PURE__ */ jsx("div", { className: "px-4 py-16 text-center lg:px-6", children: emptyState });
  }
  if (virtualizeThreshold > 0 && rows.length >= virtualizeThreshold) {
    return /* @__PURE__ */ jsx(
      DataRowListVirtualized,
      {
        rows,
        getRowId,
        renderRow,
        estimatedRowHeight,
        className,
        rowClassName,
        ariaLabel
      }
    );
  }
  return /* @__PURE__ */ jsx("ul", { "aria-label": ariaLabel, className: cn(DEFAULT_OUTER_CLASS, className), children: rows.map((row, i) => /* @__PURE__ */ jsx("li", { className: rowClassName, children: /* @__PURE__ */ jsx(DataRowListItem, { row, index: i, renderRow }) }, getRowId(row, i))) });
}
function DataRowListVirtualized({
  rows,
  getRowId,
  renderRow,
  estimatedRowHeight,
  className,
  rowClassName,
  ariaLabel
}) {
  const anchorRef = React.useRef(null);
  const [scrollMargin, setScrollMargin] = React.useState(0);
  const updateScrollMargin = React.useCallback(() => {
    const el = anchorRef.current;
    if (!el) return;
    setScrollMargin(el.getBoundingClientRect().top + window.scrollY);
  }, []);
  const updateScrollMarginRef = React.useRef(updateScrollMargin);
  updateScrollMarginRef.current = updateScrollMargin;
  React.useLayoutEffect(() => {
    const onResize = () => updateScrollMarginRef.current();
    onResize();
    window.addEventListener("resize", onResize);
    return () => window.removeEventListener("resize", onResize);
  }, [rows.length]);
  const virtualizer = useWindowVirtualizer({
    count: rows.length,
    estimateSize: () => estimatedRowHeight,
    overscan: DEFAULT_OVERSCAN,
    scrollMargin,
    getItemKey: (i) => String(getRowId(rows[i], i))
  });
  const totalSize = virtualizer.getTotalSize();
  return /* @__PURE__ */ jsx("div", { ref: anchorRef, className: cn("px-4 pb-8 pt-2 lg:px-6", className), children: /* @__PURE__ */ jsx(
    "ul",
    {
      "aria-label": ariaLabel,
      className: "relative m-0 w-full list-none p-0",
      style: { height: `${totalSize}px` },
      children: virtualizer.getVirtualItems().map((vr) => {
        const row = rows[vr.index];
        if (!row) return null;
        return /* @__PURE__ */ jsx(
          "li",
          {
            "data-index": vr.index,
            ref: virtualizer.measureElement,
            className: cn("absolute start-0 top-0 w-full pb-2", rowClassName),
            style: { transform: `translateY(${vr.start}px)` },
            children: /* @__PURE__ */ jsx(DataRowListItem, { row, index: vr.index, renderRow })
          },
          vr.key
        );
      })
    }
  ) });
}
export {
  DataRowList
};
//# sourceMappingURL=data-row-list.js.map