import { DataView , DataViewsContract } from '../../../../../../../src/plugins/data_views/public' ; import { getDataViewAndSavedSearch } from '../../util/index_utils' ; import { JobType } from '../../../../common/types/saved_objects' ; import { newJobCapsServiceAnalytics } from '../new_job_capabilities/new_job_capabilities_service_analytics' ; import { newJobCapsService } from '../new_job_capabilities/new_job_capabilities_service' ; export const ANOMALY_DETECTOR = 'anomaly-detector' ; export const DATA_FRAME_ANALYTICS = 'data-frame-analytics' ; export function loadNewJobCapabilities ( dataViewId , savedSearchId , dataViewContract , jobType ) { return new Promise ( async ( resolve , reject ) => { try { const serviceToUse = jobType === ANOMALY_DETECTOR ? newJobCapsService : newJobCapsServiceAnalytics ; if ( dataViewId !== undefined ) { const dataView = await dataViewContract . get ( dataViewId ) ; await serviceToUse . initializeFromDataVIew ( dataView ) ; resolve ( serviceToUse . newJobCaps ) ; } else if ( savedSearchId !== undefined ) { const { dataView } = await getDataViewAndSavedSearch ( savedSearchId ) ; if ( dataView === null ) { console . error ( 'Cannot retrieve data view from saved search' ) ; reject ( ) ; return ; } await serviceToUse . initializeFromDataVIew ( dataView ) ; resolve ( serviceToUse . newJobCaps ) ; } else { reject ( ) ; } } catch ( error ) { reject ( error ) ; } } ) ; }