import { defineCellType } from '../../types';
import { BooleanView } from './View';
import { BooleanEdit } from './Edit';

export { BooleanView, BooleanEdit };

export const booleanCellType = defineCellType<boolean>({
  type: 'boolean',
  ViewComponent: BooleanView,
  EditComponent: BooleanEdit,
  serialize: (value) => (value ? 'true' : 'false'),
  deserialize: (raw) => {
    const normalized = raw.trim().toLowerCase();
    if (
      normalized === '1' ||
      normalized === 'true' ||
      normalized === 'checked'
    ) {
      return true;
    }
    if (normalized === '0' || normalized === 'false' || normalized === '') {
      return false;
    }
    throw new Error();
  },
  editable: true,
  clearable: true,
  toggleValue: (value) => !value,
});
