import { i18n } from '@kbn/i18n' ; import { MB_LOOKUP_FUNCTION , VECTOR_SHAPE_TYPE , VECTOR_STYLES } from '../../../../common/constants' ; import { Category } from '../../../../common/descriptor_types' ; import { StaticTextProperty } from './properties/static_text_property' ; import { DynamicTextProperty } from './properties/dynamic_text_property' ; export function getOtherCategoryLabel ( ) { return i18n . translate ( 'xpack.maps.styles.categorical.otherCategoryLabel' , { defaultMessage : 'Other' , } ) ; } export function getComputedFieldName ( styleName , fieldName ) { return `${ getComputedFieldNamePrefix ( fieldName ) }__${ styleName as string }` ; } export function getComputedFieldNamePrefix ( fieldName ) { return `__kbn__dynamic__${ fieldName }` ; } export function isOnlySingleFeatureType ( featureType , supportedFeatures , hasFeatureType ) { if ( supportedFeatures . length === 1 ) { return supportedFeatures [ 0 ] === featureType ; } const featureTypes = Object . keys ( hasFeatureType ) ; return featureTypes . reduce ( ( accumulator , featureTypeKey ) => { const hasFeature = hasFeatureType [ featureTypeKey ] ; return featureTypeKey === featureType ? accumulator && hasFeature : accumulator && ! hasFeature ; } , true ) ; } export function dynamicRound ( value ) { if ( typeof value !== 'number' ) { return value ; } let precision = 0 ; let threshold = 10 ; while ( value < threshold && precision < 8 ) { precision ++ ; threshold = threshold / 10 ; } return precision === 0 ? Math . round ( value ) : parseFloat ( value . toFixed ( precision + 1 ) ) ; } export function assignCategoriesToPalette ( { categories , paletteValues , } ) { const stops = [ ] ; let fallbackSymbolId = null ; if ( categories . length && paletteValues . length ) { const maxLength = Math . min ( paletteValues . length , categories . length + 1 ) ; fallbackSymbolId = paletteValues [ maxLength - 1 ] ; for ( let i = 0 ; i < maxLength - 1 ; i ++ ) { stops . push ( { stop : categories [ i ] . key , style : paletteValues [ i ] , } ) ; } } return { stops , fallbackSymbolId , } ; } export function makeMbClampedNumberExpression ( { lookupFunction , fieldName , minValue , maxValue , fallback , } ) { const clamp = [ 'max' , [ 'min' , [ 'to-number' , [ lookupFunction , fieldName ] ] , maxValue ] , minValue ] ; return [ 'coalesce' , [ 'case' , [ '==' , [ lookupFunction , fieldName ] , null ] , minValue - 1 , clamp , ] , fallback , ] ; } export function getHasLabel ( label ) { return label . isDynamic ( ) ? label . isComplete ( ) : ( label as StaticTextProperty ) . getOptions ( ) . value != null && ( label as StaticTextProperty ) . getOptions ( ) . value . length ; }