/**
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License.
 */ import { NodeTypes , IOption } from './APEModels' ; import * as Util from '../../../Utils/util' ; function updateOptionNames ( value , options ) { const valueClone = JSON . parse ( JSON . stringify ( value ) ) ; updateNodeOptionNames ( valueClone . document , options ) ; return valueClone ; } function updateNodeOptionNames ( node , options , newName ? ) { if ( node . kind === 'document' ) { node . nodes . forEach ( ( n ) => updateNodeOptionNames ( n , options ) ) ; return node ; } else if ( node . kind === 'block' ) { node . nodes . forEach ( ( n ) => updateNodeOptionNames ( n , options ) ) ; return node ; } else if ( node . kind === 'inline' && node . type === NodeTypes . Mention ) { const data = node . data ; if ( ! data . completed ) { return node ; } const option = data . option ; if ( ! option ) { throw new Error ( `Attempting to update option name on inline node but it did not have option` ) ; } const matchingOption = options . find ( o => o . id === option . id ) ; if ( ! matchingOption ) { throw new Error ( `Attempting to update option name on inline node, but could not find option with id: ${ option . id }(${ option . name }) in given list of options` ) ; } option . name = matchingOption . name ; node . nodes . map ( ( n ) => updateNodeOptionNames ( n , options , matchingOption . name ) ) ; return node ; } else if ( node . kind === 'text' ) { if ( typeof newName === 'string' ) { node . leaves . forEach ( ( n ) => n . text = `$${ newName }` ) ; } return node ; } else { node . nodes . map ( ( n ) => updateNodeOptionNames ( n , options ) ) ; return node ; } } param node param entityValuesMap param replacerFn param entitiesRequired function replaceEntityNodesWithValues ( node , entityValuesMap , replacerFn , entitiesRequired ) { if ( node . kind === "value" ) { const document = replaceEntityNodesWithValues ( node . document , entityValuesMap , replacerFn , entitiesRequired ) ; node . document = document ; return node ; } if ( node . kind === 'inline' && node . type === NodeTypes . Optional ) { if ( Array . isArray ( node . nodes ) ) { node . nodes = node . nodes . map ( ( n ) => replaceEntityNodesWithValues ( n , entityValuesMap , replacerFn , false ) ) . filter ( ( n ) => n ) ; } } else { if ( node . kind === 'inline' && node . type === NodeTypes . Mention ) { const data = typeof node . data . toJS === 'function' ? node . data . toJS ( ) : node . data ; const option = data . option ; if ( option ) { const entityId = option . id ; const entityEntry = entityValuesMap [ entityId ] ; if ( entityEntry ) { const text = replacerFn ( entityEntry ) ; const textNode = { "kind" : "text" , "leaves" : [ { "kind" : "leaf" , "text" : text , "marks" : [ ] } ] } ; node . nodes = [ textNode ] ; const entityFilledData = { } ; if ( typeof entityEntry . value === "undefined" ) { entityFilledData . missing = true ; if ( entitiesRequired ) { entityFilledData . required = true ; } } else { entityFilledData . filled = true ; } node . data = { ... data , ... entityFilledData , } ; } } else { console . warn ( `Attempting to get inline node option, but node did not have option` ) ; } } if ( Array . isArray ( node . nodes ) ) { node . nodes = node . nodes . map ( ( n ) => replaceEntityNodesWithValues ( n , entityValuesMap , replacerFn , entitiesRequired ) ) . filter ( ( n ) => n ) ; } } return node ; } export default { updateOptionNames , replaceEntityNodesWithValues , } ;