import { once } from 'lodash' ; import { hydrateIndexPattern } from './hydrate_index_pattern' ; import { intializeSavedObject } from './initialize_saved_object' ; import { serializeSavedObject } from './serialize_saved_object' ; import { OpenSearchResponse , SavedObject , SavedObjectConfig , SavedObjectOpenSearchDashboardsServices , SavedObjectSaveOpts , } from '../../types' ; import { applyOpenSearchResp } from './apply_opensearch_resp' ; import { saveSavedObject } from './save_saved_object' ; export function buildSavedObject ( savedObject , config = { } , services ) { const { indexPatterns , savedObjectsClient } = services ; const opensearchType = config . type || '' ; savedObject . getDisplayName = ( ) => opensearchType ; savedObject . getOpenSearchType = ( ) => opensearchType ; type { boolean } savedObject . isSaving = false ; savedObject . defaults = config . defaults || { } ; savedObject . searchSource = config . searchSource ? services . search . searchSource . createEmpty ( ) : undefined ; savedObject . id = config . id || void 0 ; savedObject . migrationVersion = config . migrationVersion ; savedObject . copyOnSave = false ; return { Promise < IndexPattern | null > } savedObject . hydrateIndexPattern = ( id ? ) => hydrateIndexPattern ( id || '' , savedObject , indexPatterns , config ) ; return { Promise } resolved savedObject . init = once ( ( ) => intializeSavedObject ( savedObject , savedObjectsClient , config ) ) ; savedObject . applyOpenSearchResp = ( resp ) => applyOpenSearchResp ( resp , savedObject , config , services ) ; return { Object } savedObject . _serialize = ( ) => serializeSavedObject ( savedObject , config ) ; return { boolean } savedObject . isTitleChanged = ( ) => savedObject . _source && savedObject . _source . title !== savedObject . title ; savedObject . creationOpts = ( opts = { } ) => ( { id : savedObject . id , migrationVersion : savedObject . migrationVersion , ... opts , } ) ; savedObject . save = async ( opts ) => { try { const result = await saveSavedObject ( savedObject , config , opts , services ) ; return Promise . resolve ( result ) ; } catch ( e ) { return Promise . reject ( e ) ; } } ; savedObject . destroy = ( ) => { } ; return { promise } savedObject . delete = ( ) => { if ( ! savedObject . id ) { return Promise . reject ( new Error ( 'Deleting a saved Object requires type and id' ) ) ; } return savedObjectsClient . delete ( opensearchType , savedObject . id ) ; } ; }