import { INodeData , NodeKind } from "./nodeData" ; export class HierarchicalPackageNodeData implements INodeData { public static createHierarchicalNodeDataByPackageList ( packageList ) { const result = new HierarchicalPackageNodeData ( "" , "" ) ; packageList . forEach ( ( nodeData ) => result . addSubPackage ( nodeData . name . split ( "." ) , nodeData ) ) ; result . compressTree ( ) ; return result ; } public name : string ; public children : HierarchicalPackageNodeData [ ] = [ ] ; public displayName : string ; private nodeData ? : INodeData ; public get uri ( ) { return this . nodeData && this . nodeData . uri ; } public get moduleName ( ) { return this . nodeData && this . nodeData . moduleName ; } public get path ( ) { return this . nodeData && this . nodeData . path ; } public get kind ( ) { return this . nodeData ? this . nodeData . kind : NodeKind . Package ; } public get isPackage ( ) { return this . nodeData !== undefined ; } public get handlerIdentifier ( ) { return this . nodeData ?. handlerIdentifier ; } private constructor ( displayName , parentName ) { this . displayName = displayName ; this . name = parentName === "" ? displayName : parentName + "." + displayName ; } private compressTree ( ) { while ( this . name !== "" && this . children . length === 1 && ! this . isPackage ) { const child = this . children [ 0 ] ; this . name = this . name + "." + child . displayName ; this . displayName = this . displayName + "." + child . displayName ; this . children = child . children ; this . nodeData = child . nodeData ; } this . children . forEach ( ( child ) => child . compressTree ( ) ) ; } private addSubPackage ( packages , nodeData ) { if ( ! packages . length ) { this . nodeData = nodeData ; return ; } const subPackageDisplayName = packages . shift ( ) ; const childNode = this . children . find ( ( child ) => child . displayName === subPackageDisplayName ) ; if ( childNode ) { childNode . addSubPackage ( packages , nodeData ) ; } else { const newNode = new HierarchicalPackageNodeData ( subPackageDisplayName as string , this . name ) ; newNode . addSubPackage ( packages , nodeData ) ; this . children . push ( newNode ) ; } } }