import { jsxDEV } from "react/jsx-dev-runtime";
import { Entity } from "@contember/binding";
import { Table, TableRow, TableCell } from "@contember/ui";
import { useClassName } from "@contember/utilities";
import { memo, useMemo } from "react";
import { DataGridHeaderCell } from "../DataGridHeaderCell.js";
import { dataGridDictionary } from "../dataGridDictionary.js";
import { getColumnFilter } from "../getColumnFilter.js";
import { useMessageFormatter } from "../../../../../../i18n/useMessageFormatter.js";
import { EmptyMessage } from "../../../helpers/EmptyMessage.js";
const DataGridContainerTable = memo(({
  accessor,
  desiredState,
  displayedState,
  emptyMessage,
  emptyMessageComponent,
  onEntityClick,
  selectedEntityIds,
  setFilter,
  setOrderBy
}) => {
  const {
    filterArtifacts,
    orderDirections,
    columns
  } = desiredState;
  const formatMessage = useMessageFormatter(dataGridDictionary);
  const onRowClick = useMemo(() => {
    if (!onEntityClick) {
      return void 0;
    }
    return (id) => {
      const entity = accessor.getChildEntityById(id);
      if (entity) {
        onEntityClick(entity);
      }
    };
  }, [accessor, onEntityClick]);
  return /* @__PURE__ */ jsxDEV(
    Table,
    {
      className: useClassName("data-grid-body-content--table"),
      tableHead: /* @__PURE__ */ jsxDEV(TableRow, { children: Array.from(columns).filter(([columnKey]) => !desiredState.hiddenColumns[columnKey]).map(([columnKey, column]) => {
        const filterArtifact = filterArtifacts[columnKey];
        const orderDirection = orderDirections[columnKey];
        const orderColumns = Object.keys(orderDirections);
        return /* @__PURE__ */ jsxDEV(
          DataGridHeaderCell,
          {
            environment: accessor.environment,
            filterArtifact,
            emptyFilterArtifact: column.enableFiltering !== false ? column.emptyFilter : null,
            orderState: orderDirection ? {
              direction: orderDirection,
              index: orderColumns.length > 1 ? orderColumns.indexOf(columnKey) : void 0
            } : void 0,
            setFilter: (newFilter) => setFilter(columnKey, newFilter),
            setOrderBy: (newOrderBy, append = false) => setOrderBy(columnKey, newOrderBy, append),
            headerJustification: column.headerJustification || column.justification,
            shrunk: column.shrunk,
            hasFilter: getColumnFilter(column, filterArtifact, accessor.environment) !== void 0,
            header: column.header,
            ascOrderIcon: column.ascOrderIcon,
            descOrderIcon: column.descOrderIcon,
            filterRenderer: column.enableFiltering !== false ? column.filterRenderer : void 0
          },
          columnKey,
          false,
          {
            fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/base/DataGridContainer/DataGridContainerTable.tsx",
            lineNumber: 70,
            columnNumber: 9
          },
          globalThis
        );
      }) }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/base/DataGridContainer/DataGridContainerTable.tsx",
        lineNumber: 61,
        columnNumber: 5
      }, globalThis),
      children: [
        !!accessor.length && Array.from(accessor, (entity) => /* @__PURE__ */ jsxDEV(
          Entity,
          {
            accessor: entity,
            children: /* @__PURE__ */ jsxDEV(TableRow, { id: entity.id, onClick: onRowClick, active: selectedEntityIds?.includes(entity.id), children: Array.from(columns).filter(([columnKey]) => !desiredState.hiddenColumns[columnKey]).map(([columnKey, column]) => {
              if (displayedState.hiddenColumns[columnKey]) {
                return /* @__PURE__ */ jsxDEV(TableCell, { shrunk: true }, columnKey, false, {
                  fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/base/DataGridContainer/DataGridContainerTable.tsx",
                  lineNumber: 110,
                  columnNumber: 18
                }, globalThis);
              }
              return /* @__PURE__ */ jsxDEV(TableCell, { shrunk: column.shrunk, justification: column.justification, children: column.children }, columnKey, false, {
                fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/base/DataGridContainer/DataGridContainerTable.tsx",
                lineNumber: 113,
                columnNumber: 11
              }, globalThis);
            }) }, void 0, false, {
              fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/base/DataGridContainer/DataGridContainerTable.tsx",
              lineNumber: 100,
              columnNumber: 7
            }, globalThis)
          },
          entity.key,
          false,
          {
            fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/base/DataGridContainer/DataGridContainerTable.tsx",
            lineNumber: 96,
            columnNumber: 6
          },
          globalThis
        )),
        !accessor.length && /* @__PURE__ */ jsxDEV(TableRow, { children: /* @__PURE__ */ jsxDEV(TableCell, { colSpan: columns.size, children: /* @__PURE__ */ jsxDEV(
          EmptyMessage,
          {
            distinction: "seamless",
            component: emptyMessageComponent,
            children: formatMessage(emptyMessage, "dataGrid.emptyMessage.text")
          },
          void 0,
          false,
          {
            fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/base/DataGridContainer/DataGridContainerTable.tsx",
            lineNumber: 124,
            columnNumber: 7
          },
          globalThis
        ) }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/base/DataGridContainer/DataGridContainerTable.tsx",
          lineNumber: 123,
          columnNumber: 6
        }, globalThis) }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/base/DataGridContainer/DataGridContainerTable.tsx",
          lineNumber: 122,
          columnNumber: 5
        }, globalThis)
      ]
    },
    void 0,
    true,
    {
      fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/DataGrid/base/DataGridContainer/DataGridContainerTable.tsx",
      lineNumber: 58,
      columnNumber: 3
    },
    globalThis
  );
});
DataGridContainerTable.displayName = "DataGridContainerTable";
export {
  DataGridContainerTable
};
//# sourceMappingURL=DataGridContainerTable.js.map
