import { IMineMeldAPIService } from './minemeldapi' ; export interface IMinemeldPrototypeLibrary { description ? : string ; url ? : string ; prototypes ? : { [ key ] : IMinemeldPrototype ; } ; } export interface IMinemeldPrototypeMetadata { description ? : string ; nodeType ? : string ; node_type ? : string ; indicatorTypes ? : string [ ] ; tags ? : string [ ] ; developmentStatus ? : string ; author ? : string ; } export interface IMinemeldPrototype extends IMinemeldPrototypeMetadata { class : string ; config ? : any ; } export interface IMinemeldPrototypeLibraryDictionary { [ key ] : IMinemeldPrototypeLibrary ; } export interface IMinemeldPrototypeService { getPrototypeLibraries ( cancellable ? ) : angular . IPromise < any > ; getPrototypeLibrary ( library ) : angular . IPromise < any > ; getPrototype ( protofqdn ) : angular . IPromise < any > ; getPrototypeYaml ( prototypename ) : angular . IPromise < any > ; setPrototypeYaml ( prototypename , pclass , config , optionalParams ? ) : angular . IPromise < any > ; deletePrototype ( prototypename ) : angular . IPromise < any > ; invalidateCache ( ) : void ; } export class MinemeldPrototypeService implements IMinemeldPrototypeService { $state : angular . ui . IStateService ; $q : angular . IQService ; MineMeldAPIService : IMineMeldAPIService ; prototypesDict : IMinemeldPrototypeLibraryDictionary ; ngInject constructor ( $state , $q , MineMeldAPIService ) { this . $state = $state ; this . $q = $q ; this . MineMeldAPIService = MineMeldAPIService ; this . MineMeldAPIService . onLogin ( this . fillCache . bind ( this ) ) ; if ( this . MineMeldAPIService . isLoggedIn ( ) ) { this . fillCache ( ) ; } this . MineMeldAPIService . onLogout ( this . emptyCache . bind ( this ) ) ; } public getPrototypeLibraries ( cancellable ? ) { var defer ; if ( typeof cancellable === 'undefined' ) { cancellable = true ; } if ( this . prototypesDict ) { defer = this . $q . defer ( ) ; defer . resolve ( this . prototypesDict ) ; return defer . promise ; } return this . getPrototypes ( cancellable ) . then ( ( result ) => { return this . prototypesDict ; } ) ; } public getPrototypeLibrary ( library ) { var defer ; if ( this . prototypesDict ) { defer = this . $q . defer ( ) ; defer . resolve ( this . prototypesDict [ library ] ) ; return defer . promise ; } return this . getPrototypes ( ) . then ( ( result ) => { return this . prototypesDict [ library ] ; } ) ; } public getPrototype ( protofqdn ) { var defer ; var toks ; toks = protofqdn . split ( '.' ) ; if ( this . prototypesDict ) { defer = this . $q . defer ( ) ; if ( this . prototypesDict [ toks [ 0 ] ] ) { if ( this . prototypesDict [ toks [ 0 ] ] . prototypes [ toks [ 1 ] ] ) { defer . resolve ( this . prototypesDict [ toks [ 0 ] ] . prototypes [ toks [ 1 ] ] ) ; } else { defer . resolve ( undefined ) ; } } else { defer . resolve ( undefined ) ; } return defer . promise ; } return this . getPrototypes ( ) . then ( ( result ) => { if ( this . prototypesDict [ toks [ 0 ] ] ) { if ( this . prototypesDict [ toks [ 0 ] ] . prototypes [ toks [ 1 ] ] ) { defer . resolve ( this . prototypesDict [ toks [ 0 ] ] . prototypes [ toks [ 1 ] ] ) ; } else { defer . resolve ( undefined ) ; } } else { defer . resolve ( undefined ) ; } return undefined ; } ) ; } public getPrototypeYaml ( prototypename ) { var params ; var prototypeYaml ; prototypeYaml = this . MineMeldAPIService . getAPIResource ( '/prototype/:prototypename' , { } , { get : { method : 'GET' } } ) ; params = { prototypename : prototypename } ; return prototypeYaml . get ( params ) . $promise . then ( ( result ) => { if ( 'result' in result ) { return result . result ; } return { } ; } ) ; } public setPrototypeYaml ( prototypename , pclass , config , optionalParams ? ) { var prototypeYaml ; var prototype ; prototypeYaml = this . MineMeldAPIService . getAPIResource ( '/prototype/:prototypename' , { prototypename : prototypename } , { post : { method : 'POST' } } , false ) ; if ( optionalParams ) { prototype = angular . copy ( optionalParams ) ; } else { prototype = { } ; } prototype . class = pclass ; prototype . config = config ; return prototypeYaml . post ( { } , JSON . stringify ( prototype ) ) . $promise . then ( ( result ) => { if ( 'result' in result ) { return result . result ; } return { } ; } ) ; } public deletePrototype ( prototypename ) { var apiResource ; apiResource = this . MineMeldAPIService . getAPIResource ( '/prototype/:prototypename' , { prototypename : prototypename } , { delete : { method : 'DELETE' } } , false ) ; return apiResource . delete ( { } ) . $promise . then ( ( result ) => { if ( 'result' in result ) { return result . result ; } return { } ; } ) ; } public invalidateCache ( ) { this . prototypesDict = undefined ; } private getPrototypes ( cancellable ? ) { var prototypes ; if ( typeof cancellable === 'undefined' ) { cancellable = true ; } prototypes = this . MineMeldAPIService . getAPIResource ( '/prototype' , { } , { get : { method : 'GET' } } , cancellable ) ; return prototypes . get ( ) . $promise . then ( ( result ) => { this . prototypesDict = result . result ; return this . prototypesDict ; } ) ; } private fillCache ( ) { this . getPrototypes ( false ) ; } private emptyCache ( ) { this . prototypesDict = undefined ; } }