import { forIn, isArray, values } from 'lodash'; import { connectedComponents, OutboundEdges } from './graph'; import { WchTools } from './wchtools'; export function getTypeEdges(aWchTools: WchTools): OutboundEdges { // the set of edges const edges = new Set(); const cache = new Map(); const allTypes = values(aWchTools.types); function _handleElements(aElements: any) { // iterate forIn(aElements, element => { // check the type const type = element.elementType; if (type === 'reference') { // we have a type restriction if (isArray(element.restrictTypes)) { // work on these types element.restrictTypes.forEach((res: any) => edges.add(aWchTools.types[res.id])); } else { // all types allTypes.forEach(type => edges.add(type)); } } else if (type === 'group') { // recurse _handleElements(element); } }); } function _getEdges(aType: any): any[] { // test if we know the result const cachedEdges = cache.get(aType); if (isArray(cachedEdges)) { return cachedEdges; } // rest edges.clear(); // iterate over the types if (aType.elements) { // work on the elements _handleElements(aType.elements); } // ok const res = Array.from(edges); cache.set(aType, res); // ok return res; } return _getEdges; } function _isPageType(aType: any): boolean { return aType && isArray(aType.kind) && (aType.kind.indexOf('page') >= 0); } export function getPageTypes(aWchTools: WchTools): any[] { return values(aWchTools.types).filter(_isPageType); } export function getDependentTypes(aTypes: any[], aWchTools: WchTools): any[][] { // edges const edges = getTypeEdges(aWchTools); // resolve return aTypes.map(type => connectedComponents([type], edges)); } export function getPageTypeDependencies(aWchTools: WchTools): any[][] { return getDependentTypes(getPageTypes(aWchTools), aWchTools); }