"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var Table_exports = {};
__export(Table_exports, {
  ExtraRowContainer: () => ExtraRowContainer,
  RowContainer: () => RowContainer,
  Rows: () => Rows,
  Table: () => Table,
  default: () => Table_default
});
module.exports = __toCommonJS(Table_exports);
var import_react = __toESM(require("react"));
var import_styled_components = __toESM(require("styled-components"));
var import_styled_system = require("styled-system");
var import_Grid = __toESM(require("../layout/Grid"));
var import_Flex = __toESM(require("../layout/Flex"));
var Columns = __toESM(require("./Columns"));
const Container = (0, import_styled_components.default)(import_Grid.default)`
  ${import_styled_system.border}
`;
const ExtraRowContainer = import_styled_components.default.div.attrs({ gridColumn: "1 / -1" })`
  ${import_styled_system.gridColumn}
`;
const RowContainer = (0, import_styled_components.default)(import_Flex.default)`
  display: contents;
`;
function RowsComponent({
  data,
  columns,
  render,
  extraRender,
  renderRow
}) {
  return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, { key: "row-component" }, data.map((elt, rowIndex) => {
    const row = /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, columns.map((column, columnIndex) => /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, { key: `row-component-column-${rowIndex}${columnIndex}` }, render ? render({
      column,
      rowIndex,
      columnIndex,
      children: column.render({ elt, rowIndex, columnIndex })
    }) : column.render({ elt, rowIndex, columnIndex }))), extraRender && extraRender(elt, rowIndex) || null);
    if (renderRow) {
      return renderRow(rowIndex, row);
    } else {
      return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, { key: `row-component-row-${rowIndex}` }, row);
    }
  }));
}
const Rows = (0, import_react.memo)(RowsComponent);
function Table({
  data,
  columns,
  withHeaders,
  extraRender,
  renderRow,
  ...containerProps
}) {
  const gridTemplateColumns = (0, import_react.useMemo)(
    () => columns.reduce((acc, col) => `${acc} ${col.layout || "auto"}`, ""),
    [columns]
  );
  const headers = (0, import_react.useMemo)(
    () => withHeaders ? columns.reduce(
      (acc, col, index) => [
        ...acc,
        /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, { key: `header-col-${index}` }, col.header ? col.header() : /* @__PURE__ */ import_react.default.createElement("div", null))
      ],
      []
    ) : null,
    [withHeaders, columns]
  );
  return /* @__PURE__ */ import_react.default.createElement(
    Container,
    {
      gridTemplateColumns,
      gridAutoRows: "auto",
      columns: "none",
      alignItems: "center",
      ...containerProps
    },
    headers,
    /* @__PURE__ */ import_react.default.createElement(Rows, { data, columns, extraRender, renderRow })
  );
}
Table.Columns = Columns;
Table.ExtraRowContainer = ExtraRowContainer;
Table.RowContainer = RowContainer;
const memoTable = (0, import_react.memo)(Table);
memoTable.Columns = Columns;
memoTable.RowContainer = RowContainer;
memoTable.ExtraRowContainer = ExtraRowContainer;
var Table_default = memoTable;
//# sourceMappingURL=index.js.map
