import { IFormField , IFormData , TransformedData } from '@client/forms' ; import { cloneDeep , keys , omit , merge , mergeWith , isArray , first , findIndex } from 'lodash' ; export enum OBJECT_TYPE { NAME = 'name' , ADDRESS = 'address' } const nameObjectProcessor = ( objValue , srcValue ) => { if ( isArray ( objValue ) ) { const nameObj = first ( srcValue ) ; const language = nameObj && nameObj . use ? nameObj . use : '' ; const objIndex = findIndex ( objValue , { use : language } ) ; if ( objIndex < 0 ) { return objValue . concat ( srcValue ) ; } merge ( objValue [ objIndex ] , nameObj ) ; } return objValue ; } ; const addressObjectProcessor = ( objValues = [ ] , srcValues = [ ] ) => { srcValues . forEach ( ( srcValue ) => { let index = findIndex ( objValues , { type : srcValue . type } ) ; if ( index < 0 ) { objValues . push ( { type : srcValue . type , line : [ '' , '' , '' , '' , '' , '' ] } ) ; index = findIndex ( objValues , { type : srcValue . type } ) ; } for ( const key of Object . keys ( srcValue ) ) { if ( isArray ( srcValue [ key ] ) ) { ; ( srcValue [ key ] as string [ ] ) . forEach ( ( item , arrayIndex ) => { if ( item ) { objValues [ index ] [ key ] [ arrayIndex ] = item ; } } ) ; } else { objValues [ index ] [ key ] = srcValue [ key ] ; } } } ) ; return objValues ; } ; export const fieldValueNestingTransformer = ( transformedFieldName , transformerMethod ? , objectType ? ) => ( transformedData , draftData , sectionId , field ) => { if ( transformerMethod ) { transformedData [ sectionId ] [ transformedFieldName ] = transformedData [ sectionId ] [ transformedFieldName ] || { } ; const newTransformedData = cloneDeep ( transformedData ) ; const oldTransformedDataKeys = keys ( transformedData [ sectionId ] ) ; transformerMethod ( newTransformedData , draftData , sectionId , field ) ; if ( objectType === OBJECT_TYPE . NAME ) { mergeWith ( transformedData [ sectionId ] [ transformedFieldName ] , omit ( newTransformedData [ sectionId ] , oldTransformedDataKeys ) , nameObjectProcessor ) ; } else if ( objectType === OBJECT_TYPE . ADDRESS ) { mergeWith ( transformedData [ sectionId ] [ transformedFieldName ] , omit ( newTransformedData [ sectionId ] , oldTransformedDataKeys ) , addressObjectProcessor ) ; } else { merge ( transformedData [ sectionId ] [ transformedFieldName ] , omit ( newTransformedData [ sectionId ] , oldTransformedDataKeys ) ) ; } } else { transformedData [ sectionId ] [ transformedFieldName ] = transformedData [ sectionId ] [ transformedFieldName ] || { } ; transformedData [ sectionId ] [ transformedFieldName ] [ field . name ] = draftData [ sectionId ] [ field . name ] ; } return transformedData ; } ; export function setInformantSectionTransformer ( transformedData , draftData , sectionId ) { if ( draftData [ sectionId ] . _fhirIDMap && transformedData [ sectionId ] . individual ) { transformedData [ sectionId ] . individual . _fhirID = draftData [ sectionId ] . _fhirIDMap . individual ; } if ( draftData [ sectionId ] . relationship ) { transformedData [ sectionId ] . relationship = draftData [ sectionId ] . relationship ; } return transformedData ; }