import { SavedObjectsTypeMappingDefinitions } from '../../mappings' ; import { ISavedObjectTypeRegistry } from '../../saved_objects_type_registry' ; export interface CreateIndexMapOptions { kibanaIndexName : string ; registry : ISavedObjectTypeRegistry ; indexMap : SavedObjectsTypeMappingDefinitions ; } export interface IndexMap { [ index ] : { typeMappings : SavedObjectsTypeMappingDefinitions ; script ? : string ; } ; } export function createIndexMap ( { kibanaIndexName , registry , indexMap } ) { const map = { } ; Object . keys ( indexMap ) . forEach ( ( type ) => { const typeDef = registry . getType ( type ) ; const script = typeDef ?. convertToAliasScript ; const indexPattern = typeDef ?. indexPattern || kibanaIndexName ; if ( ! map . hasOwnProperty ( indexPattern as string ) ) { map [ indexPattern ] = { typeMappings : { } } ; } map [ indexPattern ] . typeMappings [ type ] = indexMap [ type ] ; if ( script && map [ indexPattern ] . script ) { throw Error ( `convertToAliasScript has been defined more than once for index pattern "${ indexPattern }"` ) ; } else if ( script ) { map [ indexPattern ] . script = script ; } } ) ; return map ; }