import { Component , ComponentProperty } from "../model/ComponentModels" ; export const Components = [ ] ; export const ComponentApi = { jsonToComponent : ( json ) => { const fromJson = JSON . parse ( json ) as Component ; const k = new Component ( fromJson ) ; return k ; } , saveComponents : ( jsons ) => { const components = jsons . map ( json => ComponentApi . jsonToComponent ( json ) ) ; Components . push ( ... components ) ; } , saveComponent : ( json ) => { const component = ComponentApi . jsonToComponent ( json ) ; Components . push ( component ) ; } , getComponents : ( ) : Component [ ] => { return Components . sort ( ( a , b ) => { if ( a . component . name < b . component . name ) { return - 1 ; } return a . component . name > b . component . name ? 1 : 0 ; } ) ; } , findByName : ( name ) : Component | undefined => { return Components . find ( ( c ) => c . component . name === name ) ; } , getComponentNameFromUri : ( uri ) : string | undefined => { return uri . split ( ":" ) [ 0 ] ; } , getUriParts : ( uri ) : Map < string , string > => { const result = new Map < string , string > ( ) ; const name = ComponentApi . getComponentNameFromUri ( uri ) ; if ( name ) { const component = ComponentApi . findByName ( name ) ; const syntax = component ?. component . syntax ; const syntaxParts = ComponentApi . parseSyntax ( syntax + '' ) ; const syntaxSeparators = ComponentApi . getSyntaxSeparators ( syntax + '' ) ; let newUri = uri === name ? name + syntaxSeparators . join ( '' ) : uri ; result . set ( name , name ) ; syntaxParts . filter ( ( x , i ) => i > 0 ) . forEach ( ( part , index ) => { if ( index < syntaxParts . length - 1 ) { const startSeparator = syntaxSeparators [ index ] ; const endSeparator = syntaxSeparators [ index + 1 ] ; const start = newUri . indexOf ( startSeparator ) + startSeparator . length ; const end = endSeparator ? newUri . indexOf ( endSeparator , start ) : newUri . length ; const val = newUri . substr ( start , end - start ) ; result . set ( part , val ) ; newUri = newUri . substr ( end ) ; } } ) ; } return result ; } , parseSyntax : ( syntax ) : string [ ] => { const separators = [ '://' , '//' , ':' , '/' , '#' ] ; let simplifiedSyntax = '' + syntax ; separators . forEach ( s => { simplifiedSyntax = simplifiedSyntax ?. replaceAll ( s , ":" ) ; } ) ; return simplifiedSyntax . split ( ":" ) ; } , getSyntaxSeparators : ( syntax ) : string [ ] => { const result = [ ] ; const parts = ComponentApi . parseSyntax ( syntax ) ; let str = '' ; parts . forEach ( ( part , index ) => { if ( index < parts . length - 1 ) { const start = syntax . indexOf ( part , str . length ) + part . length ; const end = syntax . indexOf ( parts [ index + 1 ] , start ) ; const separator = syntax . substr ( start , end - start ) ; result . push ( separator ) ; str = str + part + separator ; } } ) ; return result ; } , parseUri : ( uri ? ) : string [ ] => { const separators = [ '://' , '//' , ':' , '/' , '#' ] ; let simplifiedUri = '' + uri ; separators . forEach ( s => { simplifiedUri = simplifiedUri ?. replaceAll ( s , ":" ) ; } ) ; return simplifiedUri . split ( ":" ) ; } , getUriSeparators : ( uri ) : string [ ] => { const result = [ ] ; const name = ComponentApi . getComponentNameFromUri ( uri ) ; if ( name ) { const component = ComponentApi . findByName ( name ) ; const syntax = '' + component ?. component . syntax ; const parts = Array . from ( ComponentApi . getUriParts ( uri ) . keys ( ) ) ; let str = '' ; parts . forEach ( ( part , index ) => { if ( index < parts . length - 1 ) { const start = syntax . indexOf ( part , str . length ) + part . length ; const end = syntax . indexOf ( parts [ index + 1 ] , start ) ; const separator = syntax . substr ( start , end - start ) ; result . push ( separator ) ; str = str + part + separator ; } } ) ; } return result ; } , getPathParameterValue : ( uri , pathParameter ) : string | undefined => { return ComponentApi . getUriParts ( uri ) . get ( pathParameter ) ; } , buildComponentUri : ( uri , pathParameter , pathParameterValue ) : string | undefined => { const name = ComponentApi . getComponentNameFromUri ( uri ) ; if ( name ) { const map = ComponentApi . getUriParts ( uri ) ; map . set ( pathParameter , pathParameterValue ) ; const separators = ComponentApi . getUriSeparators ( uri ) ; const result = [ ] ; Array . from ( map . keys ( ) ) . forEach ( ( key , index ) => { const val = map . get ( key ) ; result . push ( val ? val : '' ) ; result . push ( separators [ index ] ) ; } ) ; return result . join ( '' ) ; } return uri ; } , getComponentProperties : ( componentName , type , advanced ) : ComponentProperty [ ] => { const component = ComponentApi . findByName ( componentName ) ; const properties = [ ] ; try { if ( component !== undefined ) { const map = component . properties ? new Map ( Object . entries ( component . properties ) ) : new Map ( ) ; map . forEach ( ( value , key , map ) => { const prop = new ComponentProperty ( ) ; prop . name = key ; prop . label = value . label ; prop . description = value . description ; prop . type = value . type ; prop . displayName = value . displayName ; prop . group = value . group ; prop . type = value . type ; prop . deprecated = value . deprecated ; prop . secret = value . secret ; prop . enum = value . enum ; prop . kind = value . kind ; if ( value . defaultValue ) prop . defaultValue = value . defaultValue ; if ( ! value . deprecated && value . type !== 'object' ) properties . push ( prop ) ; } ) ; } } finally { const result = [ ] ; if ( ! advanced ) { result . push ( ... properties . filter ( p => p . kind === 'path' ) ) ; result . push ( ... properties . filter ( p => p . kind !== 'path' && p . required ) ) ; result . push ( ... properties . filter ( p => p . label . length === 0 && p . kind !== 'path' && ! p . required ) ) ; result . push ( ... properties . filter ( p => p . label . startsWith ( type ) && ! p . label . includes ( "advanced" ) && ! p . required ) ) ; result . push ( ... properties . filter ( p => p . label === "formatting" && ! p . required ) ) ; } else { result . push ( ... properties . filter ( p => p . label . startsWith ( type ) && p . label . includes ( "advanced" ) ) ) ; result . push ( ... properties . filter ( p => p . label === "advanced" ) ) ; } return result ; } } , } ;