import { ModuleEngineType , ModuleComponentType } from './consts' ; interface ComponentConfigurationTypes { [ ModuleComponentType . Db ] : { } ; [ ModuleComponentType . Note ] : { } ; } export type ComponentConfiguration = { [ K in ModuleComponentType ] ? : ComponentConfigurationTypes [ K ] ; } ; interface ConfigModule { id : string ; name : string ; components : ComponentConfiguration ; syntax : string ; engine : ModuleEngineType ; } const defaultConfigData = { modules : [ ] as ConfigModule [ ] , auth : { googleClientId : '' , } , clientTopology : { staticsBaseUrl : '' , executeBaseUrl : '' , apiBasePath : '' , } , mode : { debug : false , demo : false , } } ; type ConfigData = typeof defaultConfigData ; export class ClientConfigHelper { private readonly config : ConfigData ; constructor ( initialConfig = { } ) { this . config = { ... defaultConfigData , ... initialConfig } ; } serialize ( ) { return JSON . stringify ( this . config ) ; } static load ( input ) { if ( typeof input === 'string' ) { return new ClientConfigHelper ( JSON . parse ( input ) ) ; } return new ClientConfigHelper ( input ) ; } getModule ( id ) { return this . getModules ( ) . find ( m => m . id === id ) ; } getModuleComponent < C extends ModuleComponentType > ( id , component ) { const m = this . getModule ( id ) ; return m && m . components [ component ] ; } getModules ( ) { return this . config . modules ; } addModule ( p ) { this . config . modules . push ( p ) ; return this ; } getModulesByComponent ( component ) { return this . config . modules . filter ( p => Object . keys ( p . components ) . includes ( component ) ) ; } addModuleComponent < C extends ModuleComponentType > ( moduleId , component , configuration ) { const m = this . getModule ( moduleId ) ; if ( m ) { m . components [ component ] = configuration ; } return this ; } getAuth ( ) { return this . config . auth ; } getClientTopology ( ) { return this . config . clientTopology ; } getMode ( ) { return this . config . mode ; } setAuth ( auth ) { this . config . auth = { ... auth } ; return this ; } setClientTopology ( clientTopology ) { this . config . clientTopology = { ... clientTopology } ; return this ; } setMode ( mode ) { this . config . mode = { ... mode } ; return this ; } }