import * as _ from 'lodash' ; export const TableUtils = { sortTables : ( a , b ) => ( a . display_name . default > b . display_name . default ) ? 1 : ( ( b . display_name . default > a . display_name . default ) ? - 1 : 0 ) , param tables param table param vMap findRelationsRecursive : ( tables , table , vMap ) => { vMap . set ( table . table_name , table ) ; table . relations . filter ( r => r . visible !== false ) . forEach ( rel => { const newTable = tables . find ( t => t . table_name === rel . target_table ) ; if ( ! vMap . has ( newTable . table_name ) ) { TableUtils . findRelationsRecursive ( tables , newTable , vMap ) ; } } ) ; return vMap ; } , param allTables param refferenceTable return filterTables : ( allTables , refferenceTable ) => { const tablesMap = TableUtils . findRelationsRecursive ( allTables , refferenceTable , new Map ( ) ) ; let tables = Array . from ( tablesMap . values ( ) ) ; tables = tables . sort ( TableUtils . sortTables ) ; return tables ; } , param tables param applyToAllfilter return getTablesData : ( tables , applyToAllfilter ) => { let allTables = tables . filter ( table => table . visible === true ) . sort ( TableUtils . sortTables ) ; if ( applyToAllfilter . present ) { const originTable = allTables . filter ( t => t . table_name === applyToAllfilter . refferenceTable ) [ 0 ] ; allTables = TableUtils . filterTables ( tables , originTable ) ; } let sqlOriginTables = allTables . map ( table => { return { label : table . display_name . default , value : table . table_name } ; } ) ; let tablesToShow = _ . cloneDeep ( allTables ) ; return { allTables : allTables , sqlOriginTables : sqlOriginTables , tablesToShow : tablesToShow } ; } } ;