import { IConnectionCompanyData } from './connection-map.component'; export const subsidiariesFromParentId = ( parentId: string, data: Array ) => { const subs = data.filter( companyData => companyData.parentEntityId === parentId ); return subs; }; export const getDataTree = ( DATA: Array, reqId: string ) => { const reqData = DATA.find(d => d.entityId === reqId); const { ultimateParentId } = reqData; const tree = _dataToTree(DATA, ultimateParentId); if (reqId === ultimateParentId) return tree; // level order traversal of tree let currentQueue: Array = [].concat( tree.subsidiaries ); let nextQueue: Array = []; while (currentQueue.length) { let tmp = currentQueue.shift(); if (tmp.entityId === reqId) return tmp; else nextQueue = nextQueue.concat(tmp.subsidiaries); if (currentQueue.length === 0) { currentQueue = nextQueue; nextQueue = []; } } }; const _dataToTree = ( data, companyId, parentRef = null ): IConnectionCompanyData => { const connectionCompany = data.find( companyData => companyData.entityId === companyId ); connectionCompany.parent = parentRef; connectionCompany.subsidiaries = subsidiariesFromParentId( companyId, data ).map(subsidiary => _dataToTree(data, subsidiary.entityId, connectionCompany) ); return connectionCompany; }; export interface IAvailableCountryFilter { country_iso3: IConnectionCompanyData['country_iso3']; country: IConnectionCompanyData['country']; } export interface IAvailableFilters { countries: Array; } export const getAvailableFilters = ( DATA: Array ): IAvailableFilters => { let availableCountriesHash = new Map(); DATA.forEach(connectionCompany => { if (!availableCountriesHash.has(connectionCompany.country_iso3)) { availableCountriesHash.set(connectionCompany.country_iso3, { country_iso3: connectionCompany.country_iso3, country: connectionCompany.country }); } }); return { countries: Array.from(availableCountriesHash.values()).sort( (a, b) => (a.country > b.country ? 1 : -1) ) }; };