import { defineCellType } from '../../types';
import { TextView } from './View';
import { TextEdit } from './Edit';

export { TextView, TextEdit };

export const textCellType = defineCellType<string>({
  type: 'text',
  ViewComponent: TextView,
  EditComponent: TextEdit,
  serialize: (value) => value ?? '',
  deserialize: (raw) => raw,
  editable: true,
  clearable: true,
  onTypeToEdit: ({ key, validation }) => {
    const hasSpecificValues = validation?.some((r) => r.type === 'specificValues');
    // For specific-values cells, keep editingValue as the last committed option id.
    // The typed key is forwarded via editTrigger.key so TextEditSpecificValues can
    // use it as initialSearchText without risk of committing a raw search string.
    return hasSpecificValues ? {} : { value: key };
  },
});
