import { ControlValueAccessor } from '@angular/forms' ; import { DefaultControlValueAccessorDirective } from './default-control-value-accessor.directive' ; import { NGQP_BUILT_IN_ACCESSORS } from './ngqp-accessors' ; internal export function selectValueAccessor ( valueAccessors ) { if ( ! valueAccessors || ! Array . isArray ( valueAccessors ) ) { throw new Error ( `No matching ControlValueAccessor has been found for this form control` ) ; } let defaultAccessor = null ; let builtInAccessor = null ; let customAccessor = null ; valueAccessors . forEach ( valueAccessor => { if ( valueAccessor . constructor === DefaultControlValueAccessorDirective ) { defaultAccessor = valueAccessor ; } else if ( NGQP_BUILT_IN_ACCESSORS . some ( current => valueAccessor . constructor === current ) ) { if ( builtInAccessor !== null ) { throw new Error ( `More than one built-in ControlValueAccessor matches` ) ; } builtInAccessor = valueAccessor ; } else { if ( customAccessor !== null ) { throw new Error ( `More than one custom ControlValueAccessor has been found on the form control` ) ; } customAccessor = valueAccessor ; } } ) ; if ( customAccessor !== null ) { return customAccessor ; } if ( builtInAccessor !== null ) { return builtInAccessor ; } if ( defaultAccessor !== null ) { return defaultAccessor ; } throw new Error ( `No matching ControlValueAccessor has been found for this form control` ) ; }