import { CellValue, ColumnType, ITypeOptions, Language } from '../../types'; import { exhaustiveSwitchThrow, isAbsentCellValue, isCellValueInterchangeableColumnType, isUpdatableColumnType, } from '../../typeUtils'; import { CollaboratorMatcher, transformStringToCollaboratorArray, } from '../transformString'; import { transformCellValueToAttachmentArray } from './transformCellValueToAttachmentArray'; import { transformCellValueToBoolean } from './transformCellValueToBoolean'; import { transformCellValueToDate } from './transformCellValueToDate'; import { transformCellValueToNumber } from './transformCellValueToNumber'; import { transformCellValueToRating } from './transformCellValueToRating'; import { transformCellValueToReferenceArray } from './transformCellValueToReferenceArray'; import { transformCellValueToSelectOptionId, transformCellValueToSelectOptionIdArray, } from './transformCellValueToSelect'; import { transformCellValueToStatus } from './transformCellValueToStatus'; import { transformCellValueToString } from './transformCellValueToString'; import { transformCellValueToSubtableArray } from './transformCellValueToSubtableArray'; import { ICollaboratorsById } from './types'; export const transformCellValue = < OT extends ColumnType, TT extends ColumnType >({ originCellValue, originTypeOptions: incorrectlyTypedOriginTypeOptions, targetTypeOptions: incorrectlyTypedTargetTypeOptions, opts = {}, }: { originCellValue: CellValue; originTypeOptions: ITypeOptions & { type: OT }; targetTypeOptions: ITypeOptions & { type: TT }; opts?: { locale?: Language; collaboratorsById?: ICollaboratorsById; collaboratorMatcher?: CollaboratorMatcher; }; }): CellValue | null => { const originTypeOptions = incorrectlyTypedOriginTypeOptions as ITypeOptions; const targetTypeOptions = incorrectlyTypedTargetTypeOptions as ITypeOptions; if (!isUpdatableColumnType(targetTypeOptions.type)) { throw new Error( `Can't convert cell value to non-updatable column type ${targetTypeOptions.type}` ); } if (isAbsentCellValue(originCellValue)) { throw new Error( 'transformCellValue received an invalid cell value of null or undefined' ); } const originType = originTypeOptions.type as ColumnType; const targetType = targetTypeOptions.type as ColumnType; if ( originType === targetType && isCellValueInterchangeableColumnType(targetType) ) { return originCellValue as unknown as CellValue; } switch (targetTypeOptions.type) { case ColumnType.TEXT: case ColumnType.PHONE: case ColumnType.EMAIL: return transformCellValueToString({ cellValue: originCellValue, typeOptions: originTypeOptions, opts: { locale: opts.locale, collaboratorsById: opts.collaboratorsById, }, }) as CellValue | null; case ColumnType.LONG_TEXT: return transformCellValueToString({ cellValue: originCellValue, typeOptions: originTypeOptions, opts: { preserveLongText: true, locale: opts.locale, collaboratorsById: opts.collaboratorsById, }, }) as CellValue | null; case ColumnType.NUMBER: case ColumnType.CURRENCY: return transformCellValueToNumber({ originCellValue, originTypeOptions, targetTypeOptions: targetTypeOptions as ITypeOptions, }) as CellValue | null; case ColumnType.RATING: return transformCellValueToRating({ originCellValue, originTypeOptions, targetTypeOptions: targetTypeOptions as ITypeOptions, }) as CellValue | null; case ColumnType.CHECKBOX: return transformCellValueToBoolean({ cellValue: originCellValue, typeOptions: originTypeOptions, }) as CellValue; case ColumnType.SELECT: return transformCellValueToSelectOptionId({ originCellValue, originTypeOptions, targetTypeOptions: targetTypeOptions as ITypeOptions, opts, }).optionId as CellValue | null; case ColumnType.MULTI_SELECT: return transformCellValueToSelectOptionIdArray({ originCellValue, originTypeOptions, targetTypeOptions: targetTypeOptions as ITypeOptions, opts, }).optionIds as CellValue | null; case ColumnType.STATUS: return transformCellValueToStatus({ originCellValue, originTypeOptions, targetTypeOptions: targetTypeOptions as ITypeOptions, opts }).statusId as CellValue | null; case ColumnType.MULTI_ATTACHMENT: return transformCellValueToAttachmentArray({ cellValue: originCellValue, typeOptions: originTypeOptions, }) as CellValue | null; case ColumnType.RECORD_REFERENCE: return transformCellValueToReferenceArray({ originCellValue, originTypeOptions, targetTypeOptions: targetTypeOptions as ITypeOptions, }) as CellValue | null; case ColumnType.SUBTABLE: return transformCellValueToSubtableArray({ originCellValue, originTypeOptions, targetTypeOptions: targetTypeOptions as ITypeOptions, }) as CellValue | null; case ColumnType.DATETIME: return (transformCellValueToDate({ cellValue: originCellValue, typeOptions: originTypeOptions, }) ?? null) as CellValue | null; case ColumnType.COLLABORATOR: return transformStringToCollaboratorArray({ stringValue: transformCellValueToString({ cellValue: originCellValue, typeOptions: originTypeOptions, opts: { collaboratorsById: opts.collaboratorsById, locale: opts.locale, } }) ?? '', collaboratorMatcher: opts.collaboratorMatcher ?? new CollaboratorMatcher(), }) as CellValue | null; default: return exhaustiveSwitchThrow({ switchValue: targetTypeOptions.type, }); } };