import { jsx as u } from "@emotion/react/jsx-runtime";
import { useRef as v, useMemo as T, useEffect as D } from "react";
import { CardsContainerStyled as O, CardsVirtualContainerStyled as B, CardsVirtualRowItemStyled as F } from "../../slots/DataGridSlot.js";
import { useProcessedColumns as k } from "./hooks/useProcessedColumns/useProcessedColumns.js";
import { useModalDetail as _ } from "./hooks/useModalDetail/useModalDetail.js";
import { calculateCardHeight as j } from "./helpers/calculateCardHeight/calculateCardHeight.js";
import { useCardsPerRow as G } from "./hooks/useCardsPerRow/useCardsPerRow.js";
import { useCardsVirtualizer as q } from "./hooks/useCardsVirtualizer/useCardsVirtualizer.js";
import { CardRow as A } from "./subcomponents/CardRow/CardRow.js";
function $(E) {
  const {
    rows: r,
    columns: w,
    rowKeyGetter: l,
    selectedRows: H,
    onSelectedRowsChange: d,
    onRowsChange: p,
    cardsViewConfig: e,
    focusOnRowKey: s,
    smoothScroll: g = !0,
    checkedRows: m,
    onCheckedRowsChange: R
  } = E, f = v(null), h = v(void 0), c = k(w), x = k(w, {
    applyVisibilityFilter: !1
  }), V = _({
    columns: x,
    onRowsChange: p,
    rows: r
  }), S = T(() => {
    const o = c.reduce((t, n) => {
      const a = n.renderCell?.__imageFormatterHeight;
      return a ? t + a : t;
    }, 0);
    return j({
      visibleColumnsCount: c.length,
      customMinHeight: e?.customRender?.minHeight
    }) + o;
  }, [c, e]), i = G(f), I = Math.ceil(r.length / i), { virtualizer: C, virtualItems: M, totalSize: b } = q({
    count: I,
    scrollElementRef: f,
    estimateSize: S
  });
  return D(() => {
    if (!s) {
      h.current = void 0;
      return;
    }
    if (h.current === s)
      return;
    const o = r.findIndex(
      (n) => l(n) === s
    );
    if (o === -1)
      return;
    const t = setTimeout(() => {
      const n = Math.floor(o / i);
      C.scrollToIndex(n, {
        align: "center",
        behavior: g ? "smooth" : "auto"
      }), d && d(/* @__PURE__ */ new Set([s])), h.current = s;
    }, 300);
    return () => clearTimeout(t);
  }, [
    s,
    r,
    l,
    i,
    C,
    g,
    d
  ]), c.length === 0 ? null : /* @__PURE__ */ u(O, { ownerState: {}, "data-testid": "cards-container", ref: f, children: /* @__PURE__ */ u(B, { ownerState: { totalSize: b }, children: M.map((o) => {
    const t = o.index * i, n = r.slice(t, t + i);
    return /* @__PURE__ */ u(
      F,
      {
        ref: C.measureElement,
        "data-index": o.index,
        ownerState: { offsetTop: o.start, cardsPerRow: i },
        children: n.map((a, z) => {
          const y = l(a), K = m?.has(y) || !1, P = m !== void 0 && R !== void 0;
          return /* @__PURE__ */ u(
            A,
            {
              row: a,
              rows: r,
              rowIndex: t + z,
              columns: c,
              originalColumns: x,
              rowKeyGetter: l,
              selectedRows: H,
              onSelectedRowsChange: d,
              onRowsChange: p,
              customRender: e?.customRender,
              minHeight: S,
              isChecked: K,
              showCheckbox: P,
              checkedRows: m,
              onCheckedRowsChange: R,
              onOpenDetail: V,
              showExpandButton: e && "columnsConfig" in e ? e.showExpandButton : void 0,
              onExpandClick: e && "columnsConfig" in e ? e.onExpandClick : void 0
            },
            y
          );
        })
      },
      o.key
    );
  }) }) });
}
export {
  $ as CardsView
};
