import { BlockTableRow } from "../components/tableRow";
import type { TGetItemBodyArgs } from "../config/types";

/**
 * Возвращает ряд таблицы
 * @returns
 */
export const getItemBody: TGetItemBodyArgs = ({ children, rows = [], type = "tbody" }) => {

  return children || (() => {
    const keyCounter = new Map<string, number>();
    return rows.map((row = {}, index) => {
      const keyBase = JSON.stringify({
        cols: row.cols?.length ?? 0,
        type,
        extraClasses: row.extraClasses,
      });
      const keyOrder = (keyCounter.get(keyBase) ?? 0) + 1;
      keyCounter.set(keyBase, keyOrder);
      return <BlockTableRow key={`${keyBase}_${keyOrder}`} index={index} type={type} {...row} />;
    });
  })();
};
