import { NodeBodyCreate , NODE_TYPE_FILE , NODE_TYPE_FOLDER , NODE_TITLE , NODE_DESCRIPTION } from './node-body-create' ; export interface NodeContentTree { name ? : string ; files ? : string [ ] ; folders ? : ( string | NodeContentTree ) [ ] ; title ? : string ; description ? : string ; } export function flattenNodeContentTree ( content , relativePath = '/' ) { const { name , files , folders , title , description } = content ; const aspectNames = [ 'cm:versionable' ] ; let data = [ ] ; let properties ; properties = { [ NODE_TITLE ] : title , [ NODE_DESCRIPTION ] : description } ; if ( name ) { data = data . concat ( [ { nodeType : NODE_TYPE_FOLDER , name , relativePath , properties } ] ) ; relativePath = relativePath === '/' ? `/${ name }` : `${ relativePath }/${ name }` ; } if ( folders ) { const foldersData = folders . map ( ( folder ) : NodeBodyCreate [ ] => { const folderData = typeof folder === 'string' ? { name : folder } : folder ; return flattenNodeContentTree ( folderData , relativePath ) ; } ) . reduce ( ( nodesData , folderData ) => nodesData . concat ( folderData ) , [ ] ) ; data = data . concat ( foldersData ) ; } if ( files ) { const filesData = files . map ( ( filename ) : NodeBodyCreate => ( { nodeType : NODE_TYPE_FILE , name : filename , relativePath , aspectNames } ) ) ; data = data . concat ( filesData ) ; } return data ; }