import { CoreSetup , CoreStart , Plugin } from 'src/core/public' ; import { i18n } from '@osd/i18n' ; import { UrlGeneratorId , UrlGeneratorsDefinition } from './url_generator_definition' ; import { UrlGeneratorInternal } from './url_generator_internal' ; import { UrlGeneratorContract } from './url_generator_contract' ; export interface UrlGeneratorsStart { getUrlGenerator : < T extends UrlGeneratorId > ( urlGeneratorId : T ) => UrlGeneratorContract < T > ; } export interface UrlGeneratorsSetup { registerUrlGenerator : < Id extends UrlGeneratorId > ( generator : UrlGeneratorsDefinition < Id > ) => UrlGeneratorContract < Id > ; } export class UrlGeneratorsService implements Plugin < UrlGeneratorsSetup , UrlGeneratorsStart > { private urlGenerators : Map < string , UrlGeneratorInternal < any > > = new Map ( ) ; constructor ( ) { } public setup ( core ) { const setup = { registerUrlGenerator : < Id extends UrlGeneratorId > ( generatorOptions ) => { const generator = new UrlGeneratorInternal < Id > ( generatorOptions , this . getUrlGenerator ) ; this . urlGenerators . set ( generatorOptions . id , generator ) ; return generator . getPublicContract ( ) ; } , } ; return setup ; } public start ( core ) { const start = { getUrlGenerator : this . getUrlGenerator , } ; return start ; } public stop ( ) { } private readonly getUrlGenerator = ( id ) => { const generator = this . urlGenerators . get ( id ) ; if ( ! generator ) { throw new Error ( i18n . translate ( 'share.urlGenerators.errors.noGeneratorWithId' , { defaultMessage : 'No generator found with id {id}' , values : { id } , } ) ) ; } return generator . getPublicContract ( ) ; } ; }