import { getPreference } from '../fetch' ; import { SearchStrategyProvider , SearchStrategySearchParams } from './types' ; export const defaultSearchStrategy = { id : 'default' , search : ( params ) => { return msearch ( params ) ; } , } ; function msearch ( { searchRequests , getConfig , legacy } ) { const { callMsearch , loadingCount$ } = legacy ; const requests = searchRequests . map ( ( { index , body } ) => { return { header : { index : index . title || index , preference : getPreference ( getConfig ) , } , body , } ; } ) ; const abortController = new AbortController ( ) ; let resolved = false ; loadingCount$ . next ( loadingCount$ . getValue ( ) + 1 ) ; const cleanup = ( ) => { if ( ! resolved ) { resolved = true ; loadingCount$ . next ( loadingCount$ . getValue ( ) - 1 ) ; loadingCount$ . complete ( ) ; } } ; const searching = callMsearch ( { body : { searches : requests } , signal : abortController . signal , } ) . then ( ( res ) => res ?. body ?. responses ) . finally ( ( ) => cleanup ( ) ) ; return { abort : ( ) => { abortController . abort ( ) ; cleanup ( ) ; } , searching , } ; }