import { includes } from 'lodash' ; import { IndexPatternsContract } from './index_patterns' ; import { UiSettingsCommon } from '../types' ; export type EnsureDefaultIndexPattern = ( ) => Promise < unknown > | undefined ; export const createEnsureDefaultIndexPattern = ( uiSettings , onRedirectNoIndexPattern ) => { /**
     * Checks whether a default index pattern is set and exists and defines
     * one otherwise.
     */ return async function ensureDefaultIndexPattern ( this ) { const patterns = await this . getIds ( ) ; let defaultId = await uiSettings . get ( 'defaultIndex' ) ; let defined = ! ! defaultId ; const exists = includes ( patterns , defaultId ) ; if ( defined && ! exists ) { await uiSettings . remove ( 'defaultIndex' ) ; defaultId = defined = false ; } if ( defined ) { return ; } if ( patterns . length >= 1 ) { defaultId = patterns [ 0 ] ; await uiSettings . set ( 'defaultIndex' , defaultId ) ; } else { return onRedirectNoIndexPattern ( ) ; } } ; } ;