import { HealthStateConstants } from '../Common/Constants' ; import { Utils } from './Utils' ; export interface ITextAndBadge { text : string ; badgeId : string ; badgeClass ? : string ; } export class ValueResolver { public static unknown : ITextAndBadge = { badgeId : 'Unknown' , text : 'Unknown' } ; public static get healthStatuses ( ) : ITextAndBadge [ ] { return ValueResolver . healthStatus ; } private static healthStatus : ITextAndBadge [ ] = [ { badgeId : HealthStateConstants . Invalid , text : HealthStateConstants . Invalid , badgeClass : 'badge-unknown' } , { badgeId : HealthStateConstants . OK , text : HealthStateConstants . OK , badgeClass : 'badge-ok' } , { badgeId : HealthStateConstants . Warning , text : HealthStateConstants . Warning , badgeClass : 'badge-warning' } , { badgeId : HealthStateConstants . Error , text : HealthStateConstants . Error , badgeClass : 'badge-error' } , { badgeId : HealthStateConstants . Unknown , text : HealthStateConstants . Unknown , badgeClass : 'badge-unknown' } ] ; public resolveHealthStatus ( value ) { return this . resolve ( value , ValueResolver . healthStatus , ValueResolver . healthStatus [ ValueResolver . length - 1 ] ) ; } public resolve ( value , options , defaultValue = null ) { if ( Utils . isNumeric ( value ) ) { const enumValue = + value ; if ( enumValue >= 0 && enumValue < options . length ) { return options [ enumValue ] ; } } return this . resolveEnumValue ( value , options ) || defaultValue || { badgeId : value , text : value } ; } private resolveEnumValue ( value , options ) { if ( typeof value !== 'string' ) { return null ; } return options . find ( opt => opt . text . toLowerCase ( ) === value . toLowerCase ( ) || opt . badgeId . toLowerCase ( ) === value . toLowerCase ( ) ) ; } }