/*
 * Copyright 2025 Palantir Technologies, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { getCoreRowModel, useReactTable } from "@tanstack/react-table";
import React, { useCallback, useImperativeHandle, useMemo } from "react";
import { useColumnDefs } from "./hooks/useColumnDefs.js";
import { useColumnPinning } from "./hooks/useColumnPinning.js";
import { useColumnResize } from "./hooks/useColumnResize.js";
import { useColumnVisibility } from "./hooks/useColumnVisibility.js";
import { useEditableTable } from "./hooks/useEditableTable.js";
import { useObjectTableData } from "./hooks/useObjectTableData.js";
import { useObjectTableSnapshot } from "./hooks/useObjectTableSnapshot.js";
import { useRowSelection } from "./hooks/useRowSelection.js";
import { useSelectionColumn } from "./hooks/useSelectionColumn.js";
import { useTableSorting } from "./hooks/useTableSorting.js";
import { BaseTable } from "./Table.js";
import { deriveSelectionObjectSet } from "./utils/deriveSelectionObjectSet.js";
import { getRowId, getRowIdFromPrimaryKey } from "./utils/getRowId.js";
const EMPTY_ARRAY = [];
export function ObjectTable({
  objectType,
  objectSet,
  columnDefinitions,
  filter,
  objectSetOptions,
  dedupeIntervalMs,
  pageSize,
  streamUpdates,
  orderBy,
  defaultOrderBy,
  onOrderByChanged,
  onColumnsPinnedChanged,
  onColumnResize,
  onRowSelectionChanged,
  onColumnHeaderClick,
  renderCellContextMenu,
  selectionMode = "none",
  selectedRows,
  isAllSelected: isAllSelectedProp,
  onColumnVisibilityChanged,
  onCellValueChanged,
  onSubmitEdits,
  enableOrdering = true,
  enableColumnPinning = true,
  enableColumnResizing = true,
  enableColumnConfig = true,
  editMode = "manual",
  focusedRow,
  onFocusedRowChanged,
  tableRef,
  labels,
  ...props
}) {
  const {
    columnSizing,
    onColumnSizingChange
  } = useColumnResize({
    onColumnResize
  });
  const {
    sorting,
    onSortingChange,
    orderByState
  } = useTableSorting({
    orderBy,
    defaultOrderBy,
    onOrderByChanged
  });
  const {
    data,
    fetchMore,
    isLoading,
    error,
    totalCount,
    objectSet: resultingObjectSet
  } = useObjectTableData({
    objectOrInterfaceType: objectType,
    columnDefinitions,
    filter,
    sorting,
    objectSet,
    objectSetOptions,
    dedupeIntervalMs,
    pageSize,
    streamUpdates
  });
  const {
    columns,
    loading: isColumnsLoading
  } = useColumnDefs(objectType, columnDefinitions);
  const handleRowSelectionChanged = useCallback(change => {
    if (!onRowSelectionChanged) return;
    onRowSelectionChanged({
      selectedRows: change.selectedRows,
      isSelectAll: change.isSelectAll,
      objectSet: deriveSelectionObjectSet(resultingObjectSet, change)
    });
  }, [onRowSelectionChanged, resultingObjectSet]);
  const {
    rowSelection,
    isAllSelected,
    hasSelection,
    onToggleAll,
    onToggleRow,
    enableRowSelection
  } = useRowSelection({
    selectionMode,
    selectedRows,
    isAllSelected: isAllSelectedProp,
    onRowSelectionChanged: handleRowSelectionChanged,
    data
  });
  const selectionColumn = useSelectionColumn({
    selectionMode,
    isAllSelected,
    hasSelection,
    onToggleAll,
    onToggleRow
  });
  const {
    columnVisibility,
    onColumnVisibilityChange,
    columnOrder,
    onColumnOrderChange
  } = useColumnVisibility({
    allColumns: columns,
    onColumnVisibilityChanged
  });
  const {
    columnPinning,
    onColumnPinningChange
  } = useColumnPinning({
    columnDefinitions,
    hasSelectionColumn: enableRowSelection,
    onColumnsPinnedChanged
  });
  const allColumns = useMemo(() => {
    return selectionColumn ? [selectionColumn, ...columns] : columns;
  }, [selectionColumn, columns]);
  const editableConfig = useEditableTable({
    editMode,
    onCellValueChanged,
    onSubmitEdits
  });
  const table = useReactTable({
    data: data ?? EMPTY_ARRAY,
    columns: allColumns,
    getCoreRowModel: getCoreRowModel(),
    state: {
      columnVisibility,
      columnOrder,
      rowSelection,
      sorting,
      columnSizing,
      columnPinning
    },
    onSortingChange,
    onColumnSizingChange,
    onColumnPinningChange,
    onColumnVisibilityChange,
    onColumnOrderChange,
    enableRowSelection,
    enableSorting: enableOrdering,
    columnResizeMode: "onChange",
    columnResizeDirection: "ltr",
    manualSorting: true,
    // Enable manual sorting to indicate server-side sorting
    defaultColumn: {
      minSize: 80
    },
    getRowId,
    meta: {
      onCellEdit: editableConfig.onCellEdit,
      onCellValidationError: editableConfig.onCellValidationError,
      clearCellValidationError: editableConfig.clearCellValidationError,
      cellEdits: editableConfig.cellEdits,
      isInEditMode: editableConfig.editModeState.isActive,
      validationErrors: editableConfig.validationErrors
    }
  });
  const {
    getSnapshot
  } = useObjectTableSnapshot({
    objectOrInterfaceType: objectType,
    table,
    columnDefinitions,
    objectSet: resultingObjectSet,
    pageSize,
    totalCount,
    orderBy: orderByState
  });
  useImperativeHandle(tableRef, () => ({
    getSnapshot
  }), [getSnapshot]);
  const onRenderCellContextMenu = useCallback((row, cell) => {
    return renderCellContextMenu?.(row, cell.getValue());
  }, [renderCellContextMenu]);
  const handleColumnHeaderClick = useMemo(() => onColumnHeaderClick ? columnId => onColumnHeaderClick(columnId) : undefined, [onColumnHeaderClick]);
  const headerMenuFeatureFlags = useMemo(() => ({
    showSortingItems: enableOrdering,
    showPinningItems: enableColumnPinning,
    showResizeItem: enableColumnResizing,
    showConfigItem: enableColumnConfig
  }), [enableOrdering, enableColumnPinning, enableColumnResizing, enableColumnConfig]);
  return /*#__PURE__*/React.createElement(BaseTable, {
    table: table,
    isLoading: isLoading || isColumnsLoading,
    fetchNextPage: fetchMore,
    onRowClick: props.onRowClick,
    onColumnHeaderClick: handleColumnHeaderClick,
    rowHeight: props.rowHeight,
    renderCellContextMenu: onRenderCellContextMenu,
    renderEmptyState: props.renderEmptyState,
    className: props.className,
    error: error,
    headerMenuFeatureFlags: headerMenuFeatureFlags,
    editableConfig: editableConfig,
    getRowAttributes: props.getRowAttributes,
    showEditFooter: props.showEditFooter,
    focusedRowId: focusedRow == null ? focusedRow : getRowIdFromPrimaryKey(focusedRow),
    onFocusedRowChanged: onFocusedRowChanged,
    labels: labels
  });
}
//# sourceMappingURL=ObjectTable.js.map