import { SavedObject , SavedObjectOpenSearchDashboardsServices } from '../../types' ; import { findObjectByTitle } from './find_object_by_title' ; import { SAVE_DUPLICATE_REJECTED } from '../../constants' ; import { displayDuplicateTitleConfirmModal } from './display_duplicate_title_confirm_modal' ; param savedObject param isTitleDuplicateConfirmed param onTitleDuplicate param services export async function checkForDuplicateTitle ( savedObject , isTitleDuplicateConfirmed , onTitleDuplicate , services ) { const { savedObjectsClient , overlays } = services ; if ( isTitleDuplicateConfirmed ) { return true ; } if ( savedObject . title === savedObject . lastSavedTitle && ! savedObject . copyOnSave ) { return true ; } const duplicate = await findObjectByTitle ( savedObjectsClient , savedObject . getOpenSearchType ( ) , savedObject . title ) ; if ( ! duplicate || duplicate . id === savedObject . id ) { return true ; } if ( onTitleDuplicate ) { onTitleDuplicate ( ) ; return Promise . reject ( new Error ( SAVE_DUPLICATE_REJECTED ) ) ; } return displayDuplicateTitleConfirmModal ( savedObject , overlays ) ; }