import type { Payload } from '@hapi/boom' ; import type { ISavedObjectTypeRegistry } from '../../saved_objects_type_registry' ; import type { SavedObjectsRawDoc , SavedObjectsRawDocSource } from '../../serialization' ; import type { SavedObject } from '../../types' ; import { decodeRequestVersion , encodeHitVersion } from '../../version' ; import { SavedObjectsErrorHelpers } from './errors' ; import { ALL_NAMESPACES_STRING , SavedObjectsUtils } from './utils' ; internal export type Either < L = unknown , R = L > = Left < L > | Right < R > ; /**
 * Left part of discriminated union ( Either ).
 *  internal export interface Left < L > { tag : 'Left' ; value : L ; } /**
 * Right part of discriminated union ( Either ).
 *  internal export interface Right < R > { tag : 'Right' ; value : R ; } /**
 * Type guard for left part of discriminated union ( Left ,  Either ).
 *  internal export const isLeft = < L , R > ( either ) : either is Left < L > => either . tag === 'Left' ; /**
 * Type guard for right part of discriminated union ( Right ,  Either ).
 *  internal export const isRight = < L , R > ( either ) : either is Right < R > => either . tag === 'Right' ; param type param id param rawResponse internal export function getBulkOperationError ( type , id , rawResponse ) { const { status , error } = rawResponse ; if ( error ) { switch ( status ) { case 404 : return error . type === 'index_not_found_exception' ? SavedObjectsErrorHelpers . createIndexAliasNotFoundError ( error . index ) . output . payload : SavedObjectsErrorHelpers . createGenericNotFoundError ( type , id ) . output . payload ; case 409 : return SavedObjectsErrorHelpers . createConflictError ( type , id ) . output . payload ; default : return { error : 'Internal Server Error' , message : `Unexpected bulk response [${ status }] ${ error . type }: ${ error . reason }` , statusCode : 500 , } ; } } } param version param document internal export function getExpectedVersionProperties ( version ? , document ? ) { if ( version ) { return decodeRequestVersion ( version ) ; } else if ( document ) { return { if_seq_no : document . _seq_no , if_primary_term : document . _primary_term , } ; } return { } ; } param registry param type param id param doc internal export function getSavedObjectFromSource < T > ( registry , type , id , doc ) { const { originId , updated_at : updatedAt } = doc . _source ; let namespaces = [ ] ; if ( ! registry . isNamespaceAgnostic ( type ) ) { namespaces = doc . _source . namespaces ?? [ SavedObjectsUtils . namespaceIdToString ( doc . _source . namespace ) , ] ; } return { id , type , namespaces , ... ( originId && { originId } ) , ... ( updatedAt && { updated_at : updatedAt } ) , version : encodeHitVersion ( doc ) , attributes : doc . _source [ type ] , references : doc . _source . references || [ ] , migrationVersion : doc . _source . migrationVersion , coreMigrationVersion : doc . _source . coreMigrationVersion , } ; } param registry param raw param namespace internal export function rawDocExistsInNamespace ( registry , raw , namespace ) { const rawDocType = raw . _source . type ; if ( ! registry . isMultiNamespace ( rawDocType ) ) { return true ; } const namespaces = raw . _source . namespaces ; const existsInNamespace = namespaces ?. includes ( SavedObjectsUtils . namespaceIdToString ( namespace ) ) || namespaces ?. includes ( ALL_NAMESPACES_STRING ) ; return existsInNamespace ?? false ; } param registry param raw param namespaces internal export function rawDocExistsInNamespaces ( registry , raw , namespaces ) { const rawDocType = raw . _source . type ; if ( ! registry . isMultiNamespace ( rawDocType ) ) { return true ; } const namespacesToCheck = new Set ( namespaces ) ; const existingNamespaces = raw . _source . namespaces ?? [ ] ; if ( namespacesToCheck . size === 0 || existingNamespaces . length === 0 ) { return false ; } if ( namespacesToCheck . has ( ALL_NAMESPACES_STRING ) ) { return true ; } return existingNamespaces . some ( ( x ) => x === ALL_NAMESPACES_STRING || namespacesToCheck . has ( x ) ) ; } param namespace internal export function normalizeNamespace ( namespace ? ) { if ( namespace === ALL_NAMESPACES_STRING ) { throw SavedObjectsErrorHelpers . createBadRequestError ( '"options.namespace" cannot be "*"' ) ; } else if ( namespace === undefined ) { return namespace ; } else { return SavedObjectsUtils . namespaceStringToId ( namespace ) ; } } internal export function getCurrentTime ( ) { return new Date ( Date . now ( ) ) . toISOString ( ) ; } internal export function getObjectKey ( { type , id } ) { return `${ type }:${ id }` ; } internal export function parseObjectKey ( key ) { const type = key . slice ( 0 , key . indexOf ( ':' ) ) ; const id = key . slice ( type . length + 1 ) ; if ( ! type || ! id ) { throw new Error ( 'Malformed object key (should be "type:id")' ) ; } return { type , id } ; }