import type { Map as MbMap } from '@kbn/mapbox-gl' ; import { TileMetaFeature } from '../../../common/descriptor_types' ; import { isGlDrawLayer } from './sort_layers' ; import { ILayer } from '../../classes/layers/layer' ; import { ES_MVT_META_LAYER_NAME } from '../../classes/layers/vector_layer/mvt_vector_layer/mvt_vector_layer' ; export function removeOrphanedSourcesAndLayers ( mbMap , layerList , spatialFilterLayer ) { const mbStyle = mbMap . getStyle ( ) ; if ( ! mbStyle . layers ) { return ; } const mbLayerIdsToRemove = [ ] ; mbStyle . layers . forEach ( ( mbLayer ) => { if ( spatialFilterLayer . ownsMbLayerId ( mbLayer . id ) ) { return ; } if ( isGlDrawLayer ( mbLayer . id ) ) { return ; } const targetLayer = layerList . find ( ( layer ) => { return layer . ownsMbLayerId ( mbLayer . id ) ; } ) ; if ( ! targetLayer ) { mbLayerIdsToRemove . push ( mbLayer . id ) ; } } ) ; mbLayerIdsToRemove . forEach ( ( mbLayerId ) => mbMap . removeLayer ( mbLayerId ) ) ; const mbSourcesToRemove = [ ] ; for ( const mbSourceId in mbStyle . sources ) { if ( mbStyle . sources . hasOwnProperty ( mbSourceId ) ) { if ( spatialFilterLayer . ownsMbSourceId ( mbSourceId ) ) { return ; } const targetLayer = layerList . find ( ( layer ) => { return layer . ownsMbSourceId ( mbSourceId ) ; } ) ; if ( ! targetLayer ) { mbSourcesToRemove . push ( mbSourceId ) ; } } } mbSourcesToRemove . forEach ( ( mbSourceId ) => mbMap . removeSource ( mbSourceId ) ) ; } export function getTileMetaFeatures ( mbMap , mbSourceId ) { const mbFeatures = mbMap . querySourceFeatures ( mbSourceId , { sourceLayer : ES_MVT_META_LAYER_NAME , } ) ; return mbFeatures . map ( ( mbFeature ) => { try { return { type : 'Feature' , id : mbFeature ?. id , geometry : mbFeature ?. geometry , properties : mbFeature ?. properties , } as TileMetaFeature ; } catch ( e ) { return null ; } } ) . filter ( ( mbFeature ) => mbFeature !== null ) as TileMetaFeature [ ] ; }