import { isString } from 'lodash' ; import { getExportType as getTypeCsvFromSavedObject } from '../export_types/csv_searchsource_immediate' ; import { getExportType as getTypeCsv } from '../export_types/csv_searchsource' ; import { getExportType as getTypePng } from '../export_types/png' ; import { getExportType as getTypePngV2 } from '../export_types/png_v2' ; import { getExportType as getTypePrintablePdf } from '../export_types/printable_pdf' ; import { getExportType as getTypePrintablePdfV2 } from '../export_types/printable_pdf_v2' ; import { CreateJobFn , ExportTypeDefinition } from '../types' ; type GetCallbackFn = ( item : ExportTypeDefinition ) => boolean ; export class ExportTypesRegistry { private _map : Map < string , ExportTypeDefinition > = new Map ( ) ; constructor ( ) { } register ( item ) { if ( ! isString ( item . id ) ) { throw new Error ( `'item' must have a String 'id' property ` ) ; } if ( this . _map . has ( item . id ) ) { throw new Error ( `'item' with id ${ item . id } has already been registered` ) ; } this . _map . set ( item . id , item ) ; } getAll ( ) { return Array . from ( this . _map . values ( ) ) ; } getSize ( ) { return this . _map . size ; } getById ( id ) { if ( ! this . _map . has ( id ) ) { throw new Error ( `Unknown id ${ id }` ) ; } return this . _map . get ( id ) as ExportTypeDefinition ; } get ( findType ) { let result ; for ( const value of this . _map . values ( ) ) { if ( ! findType ( value ) ) { continue ; } const foundResult = value ; if ( result ) { throw new Error ( 'Found multiple items matching predicate.' ) ; } result = foundResult ; } if ( ! result ) { throw new Error ( 'Found no items matching predicate' ) ; } return result ; } } export function getExportTypesRegistry ( ) { const registry = new ExportTypesRegistry ( ) ; type CreateFnType = CreateJobFn < any , any > ; type RunFnType = any ; const getTypeFns = [ getTypeCsv , getTypeCsvFromSavedObject , getTypePng , getTypePngV2 , getTypePrintablePdf , getTypePrintablePdfV2 , ] ; getTypeFns . forEach ( ( getType ) => { registry . register ( getType ( ) ) ; } ) ; return registry ; }