import _ from 'lodash' ; import { SOURCE_TYPES , SCALING_TYPES } from '../constants' ; import { LayerDescriptor , ESSearchSourceDescriptor } from '../descriptor_types' ; import { MapSavedObjectAttributes } from '../map_saved_object_type' ; function isEsDocumentSource ( layerDescriptor ) { const sourceType = _ . get ( layerDescriptor , 'sourceDescriptor.type' ) ; return sourceType === SOURCE_TYPES . ES_SEARCH ; } export function migrateUseTopHitsToScalingType ( { attributes , } ) { if ( ! attributes || ! attributes . layerListJSON ) { return attributes ; } let layerList = [ ] ; try { layerList = JSON . parse ( attributes . layerListJSON ) ; } catch ( e ) { throw new Error ( 'Unable to parse attribute layerListJSON' ) ; } layerList . forEach ( ( layerDescriptor ) => { if ( isEsDocumentSource ( layerDescriptor ) ) { const sourceDescriptor = layerDescriptor . sourceDescriptor as ESSearchSourceDescriptor ; sourceDescriptor . scalingType = _ . get ( layerDescriptor , 'sourceDescriptor.useTopHits' , false ) ? SCALING_TYPES . TOP_HITS : SCALING_TYPES . LIMIT ; delete sourceDescriptor . useTopHits ; } } ) ; return { ... attributes , layerListJSON : JSON . stringify ( layerList ) , } ; }