import { i18n } from '@kbn/i18n' ; import type { Logger , SavedObjectsRepository , SavedObjectsServiceStart } from 'src/core/server' ; import { SavedObjectsErrorHelpers } from '../../../../../src/core/server' ; import { DEFAULT_SPACE_ID } from '../../common/constants' ; interface Deps { getSavedObjects : ( ) => Promise < Pick < SavedObjectsServiceStart , 'createInternalRepository' > > ; logger : Logger ; } export async function createDefaultSpace ( { getSavedObjects , logger } ) { const { createInternalRepository } = await getSavedObjects ( ) ; const savedObjectsRepository = createInternalRepository ( [ 'space' ] ) ; logger . debug ( 'Checking for existing default space' ) ; const defaultSpaceExists = await doesDefaultSpaceExist ( savedObjectsRepository ) ; if ( defaultSpaceExists ) { logger . debug ( 'Default space already exists' ) ; return ; } const options = { id : DEFAULT_SPACE_ID , } ; logger . debug ( 'Creating the default space' ) ; try { await savedObjectsRepository . create ( 'space' , { name : i18n . translate ( 'xpack.spaces.defaultSpaceTitle' , { defaultMessage : 'Default' , } ) , description : i18n . translate ( 'xpack.spaces.defaultSpaceDescription' , { defaultMessage : 'This is your default space!' , } ) , color : '#00bfb3' , disabledFeatures : [ ] , _reserved : true , } , options ) ; } catch ( error ) { if ( SavedObjectsErrorHelpers . isConflictError ( error ) ) { return ; } throw error ; } logger . debug ( 'Default space created' ) ; } async function doesDefaultSpaceExist ( savedObjectsRepository ) { try { await savedObjectsRepository . get ( 'space' , DEFAULT_SPACE_ID ) ; return true ; } catch ( e ) { if ( SavedObjectsErrorHelpers . isNotFoundError ( e ) ) { return false ; } throw e ; } }