import { FieldUpdate , Type , TypeUpdate } from "facilmap-types" ; import { clone } from "facilmap-utils" ; import Vue from "vue" ; import { mergeObject } from "../../utils/utils" ; function getIdxForInsertingField ( targetFields , targetField , mergedFields ) { for ( let i = targetFields . indexOf ( targetField ) + 1 ; i < targetFields . length ; i ++ ) { if ( ! targetFields [ i ] . oldName ) continue ; let thisIdxInMergedFields = mergedFields . findIndex ( field => field . oldName == targetFields [ i ] . oldName ) ; if ( thisIdxInMergedFields != - 1 ) return thisIdxInMergedFields ; } return mergedFields . length ; } function mergeFields ( oldFields , newFields , customFields ) { let mergedFields = newFields . map ( ( newField ) => { let oldField = oldFields . find ( ( field ) => ( field . name == newField . name ) ) ; let customField = customFields . find ( ( field ) => ( field . oldName == newField . name ) ) ; if ( oldField && ! customField ) return null ; else if ( ! customField ) return Object . assign ( { } , newField , { oldName : newField . name } ) ; let mergedField = clone ( customField ) ; mergeObject ( oldField , newField , mergedField ) ; return mergedField ; } ) . filter ( field => field != null ) as FieldUpdate [ ] ; for ( let customField of customFields . filter ( field => ! field . oldName ) ) mergedFields . splice ( getIdxForInsertingField ( customFields , customField , mergedFields ) , 0 , customField ) ; return mergedFields ; } export function mergeTypeObject ( oldObject , newObject , targetObject ) { let customFields = clone ( targetObject . fields ) ; mergeObject ( oldObject , newObject , targetObject ) ; Vue . set ( targetObject , "fields" , mergeFields ( oldObject . fields , newObject . fields , customFields ) ) ; } ;