import type { IBasePath , KibanaRequest } from 'src/core/server' ; import type { Space } from '../../common' ; import { getSpaceIdFromPath } from '../../common' ; import { DEFAULT_SPACE_ID } from '../../common/constants' ; import { namespaceToSpaceId , spaceIdToNamespace } from '../lib/utils/namespace' ; import type { ISpacesClient , SpacesClientServiceStart } from '../spaces_client' ; /**
 * The Spaces service setup contract.
 */ export interface SpacesServiceSetup { param request getSpaceId ( request ) : string ; param spaceId spaceIdToNamespace ( spaceId ) : string | undefined ; param namespace namespaceToSpaceId ( namespace ) : string ; } /**
 * The Spaces service start contract.
 */ export interface SpacesServiceStart { param request createSpacesClient : ( request : KibanaRequest ) => ISpacesClient ; param request getSpaceId ( request ) : string ; param request isInDefaultSpace ( request ) : boolean ; param request getActiveSpace ( request ) : Promise < Space > ; param spaceId spaceIdToNamespace ( spaceId ) : string | undefined ; param namespace namespaceToSpaceId ( namespace ) : string ; } interface SpacesServiceSetupDeps { basePath : IBasePath ; } interface SpacesServiceStartDeps { basePath : IBasePath ; spacesClientService : SpacesClientServiceStart ; } /**
 * Service for interacting with spaces.
 */ export class SpacesService { public setup ( { basePath } ) { return { getSpaceId : ( request ) => { return this . getSpaceId ( request , basePath ) ; } , spaceIdToNamespace , namespaceToSpaceId , } ; } public start ( { basePath , spacesClientService } ) { return { getSpaceId : ( request ) => { return this . getSpaceId ( request , basePath ) ; } , getActiveSpace : ( request ) => { const spaceId = this . getSpaceId ( request , basePath ) ; return spacesClientService . createSpacesClient ( request ) . get ( spaceId ) ; } , isInDefaultSpace : ( request ) => { const spaceId = this . getSpaceId ( request , basePath ) ; return spaceId === DEFAULT_SPACE_ID ; } , createSpacesClient : ( request ) => spacesClientService . createSpacesClient ( request ) , spaceIdToNamespace , namespaceToSpaceId , } ; } public stop ( ) { } private getSpaceId ( request , basePathService ) { const basePath = basePathService . get ( request ) ; const { spaceId } = getSpaceIdFromPath ( basePath , basePathService . serverBasePath ) ; return spaceId ; } }