import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { DefaultTableCell } from '../default-cell.js';
import { Form } from '../../../../scalars/components/form/form.js';
import { cn } from '../../../../scalars/lib/utils.js';
import { EmptyCell } from './empty-cell.js';
import { useCellLogic } from './use-cell-logic.js';
const RenderCell = ({ rowItem, column, rowIndex, columnIndex, renderEmptyCell = false, }) => {
    const { config, isThisCellSelected, isCellEditMode, handleCellClick, formRef, hasErrors } = useCellLogic({
        rowIndex,
        columnIndex,
        renderEmptyCell,
    });
    /**
     * If the cell is empty, we don't want to render anything
     * so we're going to return a default table cell with no content
     */
    if (renderEmptyCell) {
        return _jsx(EmptyCell, { column: column, isSelected: isThisCellSelected, onClick: handleCellClick });
    }
    const cellContext = {
        row: rowItem.data,
        column,
        rowIndex,
        columnIndex,
        tableConfig: config,
    };
    // get and format the cell value
    const cellValue = column.valueFormatter?.(column.valueGetter?.(rowItem.data, cellContext), cellContext);
    const isThisCellEditMode = isCellEditMode && isThisCellSelected;
    const isAddingRow = rowIndex === config.data.length;
    return (_jsx(DefaultTableCell, { onClick: handleCellClick, isSelected: isThisCellSelected, isEditable: (column.editable ?? false) || (isAddingRow && !!column.renderCellEditor), isEditing: isThisCellEditMode, hasErrors: hasErrors, children: _jsxs(Form, { ref: formRef, onSubmit: () => undefined, submitChangesOnly: true, extraFormProps: { mode: 'all', shouldUnregister: true }, children: [_jsx("div", { className: cn({ hidden: !isThisCellEditMode }, 
                    // errors and warnings messages
                    '[&_[data-type="error"]]:hidden [&_[data-type="warning"]]:hidden', 
                    // character counter
                    '[&_[data-type="character-counter"]]:hidden'), children: column.renderCellEditor?.(cellValue, () => null, cellContext) }), _jsx("div", { className: cn({ hidden: isThisCellEditMode }), children: column.renderCell?.(cellValue, cellContext) })] }) }, column.field));
};
export { RenderCell };
//# sourceMappingURL=render-cell.js.map