import { get } from 'lodash' ; import { i18n } from '@osd/i18n' ; import { SavedObjectAttributes , SavedObjectsCreateOptions , OverlayStart , SavedObjectsClientContract , } from 'opensearch-dashboards/public' ; import { OVERWRITE_REJECTED } from '../../constants' ; import { confirmModalPromise } from './confirm_modal_promise' ; param source param savedObject param options param services returns { Promise } resolved export async function saveWithConfirmation ( source , savedObject , options , services ) { const { savedObjectsClient , overlays } = services ; try { return await savedObjectsClient . create ( savedObject . getOpenSearchType ( ) , source , options ) ; } catch ( err ) { if ( get ( err , 'res.status' ) === 409 ) { const confirmMessage = i18n . translate ( 'savedObjects.confirmModal.overwriteConfirmationMessage' , { defaultMessage : 'Are you sure you want to overwrite {title}?' , values : { title : savedObject . title } , } ) ; const title = i18n . translate ( 'savedObjects.confirmModal.overwriteTitle' , { defaultMessage : 'Overwrite {name}?' , values : { name : savedObject . displayName } , } ) ; const confirmButtonText = i18n . translate ( 'savedObjects.confirmModal.overwriteButtonLabel' , { defaultMessage : 'Overwrite' , } ) ; return confirmModalPromise ( confirmMessage , title , confirmButtonText , overlays ) . then ( ( ) => savedObjectsClient . create ( savedObject . getOpenSearchType ( ) , source , { overwrite : true , ... options , } ) ) . catch ( ( ) => Promise . reject ( new Error ( OVERWRITE_REJECTED ) ) ) ; } return await Promise . reject ( err ) ; } }