import { SavedObjectReference } from '../../../../../src/core/types' ; import { MapSavedObjectAttributes } from '../map_saved_object_type' ; import { LayerDescriptor , VectorLayerDescriptor } from '../descriptor_types' ; interface IndexPatternReferenceDescriptor { indexPatternId ? : string ; indexPatternRefName ? : string ; } export function extractReferences ( { attributes , references = [ ] , } ) { if ( ! attributes . layerListJSON ) { return { attributes , references } ; } const extractedReferences = [ ] ; let layerList = [ ] ; try { layerList = JSON . parse ( attributes . layerListJSON ) ; } catch ( e ) { throw new Error ( 'Unable to parse attribute layerListJSON' ) ; } layerList . forEach ( ( layer , layerIndex ) => { if ( layer . sourceDescriptor && 'indexPatternId' in layer . sourceDescriptor ) { const sourceDescriptor = layer . sourceDescriptor as IndexPatternReferenceDescriptor ; const refName = `layer_${ layerIndex }_source_index_pattern` ; extractedReferences . push ( { name : refName , type : 'index-pattern' , id : sourceDescriptor . indexPatternId ! , } ) ; delete sourceDescriptor . indexPatternId ; sourceDescriptor . indexPatternRefName = refName ; } if ( 'joins' in layer ) { const vectorLayer = layer as VectorLayerDescriptor ; const joins = vectorLayer . joins ? vectorLayer . joins : [ ] ; joins . forEach ( ( join , joinIndex ) => { if ( 'indexPatternId' in join . right ) { const sourceDescriptor = join . right as IndexPatternReferenceDescriptor ; const refName = `layer_${ layerIndex }_join_${ joinIndex }_index_pattern` ; extractedReferences . push ( { name : refName , type : 'index-pattern' , id : sourceDescriptor . indexPatternId ! , } ) ; delete sourceDescriptor . indexPatternId ; sourceDescriptor . indexPatternRefName = refName ; } } ) ; } } ) ; return { attributes : { ... attributes , layerListJSON : JSON . stringify ( layerList ) , } , references : references . concat ( extractedReferences ) , } ; } function findReference ( targetName , references ) { const reference = references . find ( ( { name } ) => name === targetName ) ; if ( ! reference ) { throw new Error ( `Could not find reference "${ targetName }"` ) ; } return reference ; } export function injectReferences ( { attributes , references , } ) { if ( ! attributes . layerListJSON ) { return { attributes } ; } let layerList = [ ] ; try { layerList = JSON . parse ( attributes . layerListJSON ) ; } catch ( e ) { throw new Error ( 'Unable to parse attribute layerListJSON' ) ; } layerList . forEach ( ( layer ) => { if ( layer . sourceDescriptor && 'indexPatternRefName' in layer . sourceDescriptor ) { const sourceDescriptor = layer . sourceDescriptor as IndexPatternReferenceDescriptor ; const reference = findReference ( sourceDescriptor . indexPatternRefName ! , references ) ; sourceDescriptor . indexPatternId = reference . id ; delete sourceDescriptor . indexPatternRefName ; } if ( 'joins' in layer ) { const vectorLayer = layer as VectorLayerDescriptor ; const joins = vectorLayer . joins ? vectorLayer . joins : [ ] ; joins . forEach ( ( join ) => { if ( 'indexPatternRefName' in join . right ) { const sourceDescriptor = join . right as IndexPatternReferenceDescriptor ; const reference = findReference ( sourceDescriptor . indexPatternRefName ! , references ) ; sourceDescriptor . indexPatternId = reference . id ; delete sourceDescriptor . indexPatternRefName ; } } ) ; } } ) ; return { attributes : { ... attributes , layerListJSON : JSON . stringify ( layerList ) , } , } ; }