import { ServiceStatus , ServiceStatusLevels , ServiceStatusLevel } from './types' ; /**
 * Returns a single  ServiceStatus that summarizes the most severe status level from a group of statuses.
  export const getSummaryStatus = ( statuses , { allAvailableSummary = `All services are available` , maxServices = 3 , } = { } ) : ServiceStatus => { const { highestLevel , highestStatuses } = highestLevelSummary ( statuses ) ; if ( highestLevel === ServiceStatusLevels . available ) { return { level : ServiceStatusLevels . available , summary : allAvailableSummary , } ; } else { const affectedServices = highestStatuses . map ( ( [ serviceName ] ) => serviceName ) ; return { level : highestLevel , summary : getSummaryContent ( affectedServices , highestLevel , maxServices ) , detail : `See the status page for more information` , meta : { affectedServices , } , } ; } } ; const getSummaryContent = ( affectedServices , statusLevel , maxServices ) : string => { const serviceCount = affectedServices . length ; if ( serviceCount === 1 ) { return `1 service is ${ statusLevel . toString ( ) }: ${ affectedServices [ 0 ] }` ; } else if ( serviceCount > maxServices ) { const exceedingCount = serviceCount - maxServices ; return `${ serviceCount } services are ${ statusLevel . toString ( ) }: ${ affectedServices . slice ( 0 , maxServices ) . join ( ', ' ) } and ${ exceedingCount } other(s)` ; } else { return `${ serviceCount } services are ${ statusLevel . toString ( ) }: ${ affectedServices . join ( ', ' ) }` ; } } ; type StatusPair = [ string , ServiceStatus ] ; const highestLevelSummary = ( statuses ) : { highestLevel : ServiceStatusLevel ; highestStatuses : StatusPair [ ] ; } => { let highestLevel = ServiceStatusLevels . available ; let highestStatuses = [ ] ; for ( const pair of statuses ) { if ( pair [ 1 ] . level === highestLevel ) { highestStatuses . push ( pair ) ; } else if ( pair [ 1 ] . level > highestLevel ) { highestLevel = pair [ 1 ] . level ; highestStatuses = [ pair ] ; } } return { highestLevel , highestStatuses , } ; } ;