import { ODataParserOptions } from '../../options' ; import { raw } from '../../resources/query' ; import { EnumTypeConfig , EnumTypeFieldConfig , Options , OptionsHelper , Parser , } from '../../types' ; import { Enums , Strings } from '../../utils' ; import { ODataAnnotatable } from '../annotation' ; export class ODataEnumTypeFieldParser extends ODataAnnotatable { name : string ; value : number ; constructor ( name , field ) { super ( field ) ; this . name = name ; this . value = field . value ; } titleize ( term ? ) { return ( term && this . annotatedValue ( term ) ) || this . name ; } } export class ODataEnumTypeParser < T > extends ODataAnnotatable implements Parser < T > { name : string ; namespace : string ; alias ? : string ; flags ? : boolean ; members : { [ name ] : number ; } | { [ value ] : string ; } ; fields : ODataEnumTypeFieldParser [ ] ; optionsHelper ? : OptionsHelper ; constructor ( config , namespace , alias ? ) { super ( config ) ; this . name = config . name ; this . namespace = namespace ; this . alias = alias ; this . flags = config . flags ; this . members = config . members ; this . fields = Object . entries ( config . fields ) . map ( ( [ name , f ] ) => new ODataEnumTypeFieldParser ( name , f ) ) ; } param term returns ttitelize ( term ? ) { return ( term && this . annotatedValue ( term ) ) || Strings . titleCase ( this . name ) ; } configure ( { options } ) { this . optionsHelper = options ; } isTypeOf ( type ) { var names = [ `${ this . namespace }.${ this . name }` ] ; if ( this . alias ) names . push ( `${ this . alias }.${ this . name }` ) ; return names . indexOf ( type ) !== - 1 ; } deserialize ( value , options ? ) { const parserOptions = options !== undefined ? new ODataParserOptions ( options ) : this . optionsHelper ; if ( this . flags ) { return Enums . toValues ( this . members , value ) . reduce ( ( acc , v ) => acc | v , 0 ) as any ; } else { return Enums . toValue ( this . members , value ) as any ; } } serialize ( value , options ? ) { const parserOptions = options !== undefined ? new ODataParserOptions ( options ) : this . optionsHelper ; if ( this . flags ) { const names = Enums . toNames ( this . members , value ) ; return ! parserOptions ?. stringAsEnum ? `${ this . namespace }.${ this . name }'${ names . join ( ', ' ) }'` : names . join ( ', ' ) ; } else { const name = Enums . toName ( this . members , value ) ; return ! parserOptions ?. stringAsEnum ? `${ this . namespace }.${ this . name }'${ name }'` : name ; } } encode ( value , options ? ) { const parserOptions = options !== undefined ? new ODataParserOptions ( options ) : this . optionsHelper ; const serialized = this . serialize ( value , parserOptions ) ; return parserOptions ?. stringAsEnum ? raw ( `'${ serialized }'` ) : raw ( serialized ) ; } toJsonSchema ( ) { let property = < any > { title : this . name , type : 'string' , } ; property . enum = this . fields . map ( ( f ) => f . name ) ; return property ; } validate ( member , { method , navigation = false , } = { } ) { if ( this . flags ) { let members = Enums . toValues ( this . members , member ) ; return members . some ( ( member ) => ! ( member in this . members ) ) ? [ 'mismatch' ] : undefined ; } else { return ! ( member in this . members ) ? [ 'mismatch' ] : undefined ; } } }