import { jsx as c } from "@emotion/react/jsx-runtime";
import { useRef as x, useEffect as K } from "react";
import { ListContainerStyled as T, ListVirtualContainerStyled as b, ListRowVirtualizedItemStyled as D } from "../../slots/DataGridSlot.js";
import { useProcessedColumns as S } from "./hooks/useProcessedColumns/useProcessedColumns.js";
import { useModalDetail as I } from "./hooks/useModalDetail/useModalDetail.js";
import { useListVirtualizer as O } from "./hooks/useListVirtualizer/useListVirtualizer.js";
import { ListRow as B } from "./subcomponents/ListRow/ListRow.js";
function A(g) {
  const {
    rows: t,
    columns: m,
    rowKeyGetter: i,
    selectedRows: v,
    onSelectedRowsChange: r,
    onRowsChange: f,
    focusOnRowKey: n,
    smoothScroll: w = !0,
    checkedRows: d,
    onCheckedRowsChange: h,
    listViewConfig: e
  } = g, p = x(null), u = x(void 0), R = S(m), C = S(m, {
    applyVisibilityFilter: !1
  }), y = I({
    columns: C,
    onRowsChange: f,
    rows: t
  }), E = e && "customRender" in e && e.customRender ? e.customRender.minHeight : void 0, { virtualizer: a, virtualItems: k, totalSize: L } = O({
    count: t.length,
    scrollElementRef: p,
    estimateSize: E
  });
  return K(() => {
    if (!n) {
      u.current = void 0;
      return;
    }
    if (u.current === n)
      return;
    const o = t.findIndex(
      (l) => i(l) === n
    );
    if (o === -1)
      return;
    const s = setTimeout(() => {
      a.scrollToIndex(o, { align: "center", behavior: w ? "smooth" : "auto" }), r && r(/* @__PURE__ */ new Set([n])), u.current = n;
    }, 300);
    return () => clearTimeout(s);
  }, [
    n,
    t,
    i,
    r,
    a,
    w
  ]), R.length === 0 ? null : /* @__PURE__ */ c(
    T,
    {
      ref: p,
      ownerState: {},
      "data-testid": "list-view",
      children: /* @__PURE__ */ c(b, { ownerState: { totalSize: L }, children: k.map((o) => {
        const s = t[o.index], l = i(s), V = d?.has(l) ?? !1, z = d !== void 0 && h !== void 0;
        return /* @__PURE__ */ c(
          D,
          {
            ref: a.measureElement,
            ownerState: { offsetTop: o.start },
            "data-index": o.index,
            children: /* @__PURE__ */ c(
              B,
              {
                row: s,
                rows: t,
                rowIndex: o.index,
                columns: R,
                originalColumns: C,
                rowKeyGetter: i,
                selectedRows: v,
                onSelectedRowsChange: r,
                onRowsChange: f,
                customRender: e && "customRender" in e ? e.customRender : void 0,
                isChecked: V,
                showCheckbox: z,
                checkedRows: d,
                onCheckedRowsChange: h,
                onOpenDetail: y,
                showExpandButton: e && "columnsConfig" in e ? e.showExpandButton : void 0,
                onExpandClick: e && "columnsConfig" in e ? e.onExpandClick : void 0
              }
            )
          },
          l
        );
      }) })
    }
  );
}
export {
  A as ListView
};
