import { memo, useMemo, createElement } from "react";
import { Entity } from "@contember/binding";
import { Table, useComponentClassName, TableRow, TableCell } from "@contember/ui";
import { dataGridDictionary } from "../dataGridDictionary.js";
import { DataGridHeaderCell } from "../DataGridHeaderCell.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__ */ createElement(Table, {
    className: useComponentClassName("data-grid-body-content--table"),
    tableHead: /* @__PURE__ */ createElement(TableRow, null, 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__ */ createElement(DataGridHeaderCell, {
        key: columnKey,
        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
      });
    }))
  }, !!accessor.length && Array.from(accessor, (entity) => /* @__PURE__ */ createElement(Entity, {
    key: entity.key,
    accessor: entity
  }, /* @__PURE__ */ createElement(TableRow, {
    id: entity.id,
    onClick: onRowClick,
    active: selectedEntityIds?.includes(entity.id)
  }, Array.from(columns).filter(([columnKey]) => !desiredState.hiddenColumns[columnKey]).map(([columnKey, column]) => {
    if (displayedState.hiddenColumns[columnKey]) {
      return /* @__PURE__ */ createElement(TableCell, {
        key: columnKey,
        shrunk: true
      });
    }
    return /* @__PURE__ */ createElement(TableCell, {
      key: columnKey,
      shrunk: column.shrunk,
      justification: column.justification
    }, column.children);
  })))), !accessor.length && /* @__PURE__ */ createElement(TableRow, null, /* @__PURE__ */ createElement(TableCell, {
    colSpan: columns.size
  }, /* @__PURE__ */ createElement(EmptyMessage, {
    distinction: "seamless",
    component: emptyMessageComponent
  }, formatMessage(emptyMessage, "dataGrid.emptyMessage.text")))));
});
DataGridContainerTable.displayName = "DataGridContainerTable";
export { DataGridContainerTable };
//# sourceMappingURL=DataGridContainerTable.js.map
