import { jsx as m } from "@emotion/react/jsx-runtime";
import { useRef as x, useMemo as S, useEffect as b } from "react";
import { CardsContainerStyled as K } from "../../slots/DataGridSlot.js";
import { useProcessedColumns as y } from "./hooks/useProcessedColumns/useProcessedColumns.js";
import { useModalDetail as D } from "./hooks/useModalDetail/useModalDetail.js";
import { calculateCardHeight as F } from "./helpers/calculateCardHeight/calculateCardHeight.js";
import { scrollToRowElement as I } from "../../helpers/shared/scrollToRowElement/scrollToRowElement.js";
import { ContainerFlow as M } from "../../../ContainerFlow/ContainerFlow.js";
import { CardRow as O } from "./subcomponents/CardRow/CardRow.js";
function z(v) {
  const {
    rows: i,
    columns: f,
    rowKeyGetter: s,
    selectedRows: k,
    onSelectedRowsChange: l,
    onRowsChange: h,
    cardsViewConfig: e,
    focusOnRowKey: n,
    smoothScroll: C = !0,
    checkedRows: u,
    onCheckedRowsChange: w
  } = v, p = x(null), d = x(void 0), c = y(f), g = y(f, {
    applyVisibilityFilter: !1
  }), H = D({
    columns: g,
    onRowsChange: h,
    rows: i
  }), R = S(() => {
    const t = c.reduce((r, o) => {
      const a = o.renderCell?.__imageFormatterHeight;
      return a ? r + a : r;
    }, 0);
    return F({
      visibleColumnsCount: c.length,
      customMinHeight: e?.customRender?.minHeight
    }) + t;
  }, [c, e]);
  return b(() => {
    if (!n) {
      d.current = void 0;
      return;
    }
    if (d.current === n || i.findIndex(
      (o) => s(o) === n
    ) === -1)
      return;
    const r = setTimeout(() => {
      const o = p.current;
      if (!o) {
        console.warn("container es null, abortando scroll");
        return;
      }
      I(
        o,
        n,
        "data-attribute",
        "data-row-key",
        C
      ), l && l(/* @__PURE__ */ new Set([n])), d.current = n;
    }, 300);
    return () => clearTimeout(r);
  }, [n, i, s, R, l, C]), c.length === 0 ? null : /* @__PURE__ */ m(K, { ownerState: {}, "data-testid": "cards-container", ref: p, children: /* @__PURE__ */ m(M, { variant: "grid-layout", minWidth: 280, children: i.map((t, r) => {
    const o = s(t), a = u?.has(o) || !1, E = u !== void 0 && w !== void 0;
    return /* @__PURE__ */ m(
      O,
      {
        row: t,
        rows: i,
        rowIndex: r,
        columns: c,
        originalColumns: g,
        rowKeyGetter: s,
        selectedRows: k,
        onSelectedRowsChange: l,
        onRowsChange: h,
        customRender: e?.customRender,
        minHeight: R,
        isChecked: a,
        showCheckbox: E,
        checkedRows: u,
        onCheckedRowsChange: w,
        onOpenDetail: H,
        showExpandButton: e && "columnsConfig" in e ? e.showExpandButton : void 0,
        onExpandClick: e && "columnsConfig" in e ? e.onExpandClick : void 0
      },
      s(t)
    );
  }) }) });
}
export {
  z as CardsView
};
