import type * as estypes from '@elastic/elasticsearch/lib/api/typesWithBodyKey' ; import type { SortFieldOrUndefined } from '@kbn/securitysolution-io-ts-list-types' ; export type TieBreaker < T > = T & { tie_breaker_id : string ; } ; interface GetSearchAfterWithTieBreakerOptions < T > { response : estypes . SearchResponse < TieBreaker < T > > ; sortField : SortFieldOrUndefined ; } export const getSearchAfterWithTieBreaker = < T > ( { response , sortField , } ) : string [ ] | undefined => { if ( response . hits . hits . length === 0 ) { return undefined ; } else { const lastEsElement = response . hits . hits [ response . hits . hits . length - 1 ] ; if ( sortField == null ) { return [ lastEsElement . _source . tie_breaker_id ] ; } else { const [ [ , sortValue ] ] = Object . entries ( lastEsElement . _source ) . filter ( ( [ key ] ) => key === sortField ) ; if ( typeof sortValue === 'string' ) { return [ sortValue , lastEsElement . _source . tie_breaker_id ] ; } else { return [ lastEsElement . _source . tie_breaker_id ] ; } } } } ;