/*
 * 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 { flexRender } from "@tanstack/react-table";
import React, { useRef } from "react";
import { CellContextMenu } from "./CellContextMenu.js";
import { useCellContextMenu } from "./hooks/useCellContextMenu.js";
import { SELECTION_COLUMN_ID } from "./utils/constants.js";
import { isCellEditable } from "./utils/editableUtils.js";
import { getColumnPinningStyles } from "./utils/getColumnPinningStyles.js";
import { shouldShowEditableCell } from "./utils/shouldShowEditableCell.js";
import styles from "./TableCell.module.css";
export function TableCell({
  cell,
  renderCellContextMenu
}) {
  const tdRef = useRef(null);
  const isSelectColumn = cell.column.id === SELECTION_COLUMN_ID;
  const {
    isContextMenuOpen,
    handleOpenContextMenu,
    handleCloseContextMenu,
    popoverPosition
  } = useCellContextMenu({
    tdRef
  });
  const {
    columnStyles
  } = getColumnPinningStyles(cell.column);
  const tableMeta = cell.getContext().table.options.meta;
  const columnMeta = cell.column.columnDef.meta;
  const isEditable = shouldShowEditableCell(isCellEditable(columnMeta?.editable, cell.row.original), tableMeta?.onCellEdit, tableMeta?.isInEditMode);
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("td", {
    ref: tdRef,
    "data-pinned": cell.column.getIsPinned(),
    "data-editable": isEditable ? "true" : undefined,
    className: styles.osdkTableCell,
    style: columnStyles,
    onContextMenu: handleOpenContextMenu
  }, /*#__PURE__*/React.createElement("div", {
    className: styles.osdkTableCellContent
  }, flexRender(cell.column.columnDef.cell, cell.getContext()))), !isSelectColumn && isContextMenuOpen && !!popoverPosition && !!renderCellContextMenu && /*#__PURE__*/React.createElement(CellContextMenu, {
    cell: cell,
    position: popoverPosition,
    onClose: handleCloseContextMenu,
    renderContent: renderCellContextMenu
  }));
}
//# sourceMappingURL=TableCell.js.map