import * as ERROR_MSGS from "../constants/error_msgs" ; import { BindingScopeEnum , BindingTypeEnum } from "../constants/literal_types" ; import { interfaces } from "../interfaces/interfaces" ; import { BindingInWhenOnSyntax } from "./binding_in_when_on_syntax" ; import { BindingWhenOnSyntax } from "./binding_when_on_syntax" ; class BindingToSyntax < T > implements interfaces . BindingToSyntax < T > { private _binding : interfaces . Binding < T > ; public constructor ( binding ) { this . _binding = binding ; } public to ( constructor ) { this . _binding . type = BindingTypeEnum . Instance ; this . _binding . implementationType = constructor ; return new BindingInWhenOnSyntax < T > ( this . _binding ) ; } public toSelf ( ) { if ( typeof this . _binding . serviceIdentifier !== "function" ) { throw new Error ( `${ ERROR_MSGS . INVALID_TO_SELF_VALUE }` ) ; } const self = this . _binding . serviceIdentifier ; return this . to ( self ) ; } public toConstantValue ( value ) { this . _binding . type = BindingTypeEnum . ConstantValue ; this . _binding . cache = value ; this . _binding . dynamicValue = null ; this . _binding . implementationType = null ; this . _binding . scope = BindingScopeEnum . Singleton ; return new BindingWhenOnSyntax < T > ( this . _binding ) ; } public toDynamicValue ( func ) { this . _binding . type = BindingTypeEnum . DynamicValue ; this . _binding . cache = null ; this . _binding . dynamicValue = func ; this . _binding . implementationType = null ; return new BindingInWhenOnSyntax < T > ( this . _binding ) ; } public toConstructor < T2 > ( constructor ) { this . _binding . type = BindingTypeEnum . Constructor ; this . _binding . implementationType = constructor as unknown as T ; this . _binding . scope = BindingScopeEnum . Singleton ; return new BindingWhenOnSyntax < T > ( this . _binding ) ; } public toFactory < T2 > ( factory ) { this . _binding . type = BindingTypeEnum . Factory ; this . _binding . factory = factory ; this . _binding . scope = BindingScopeEnum . Singleton ; return new BindingWhenOnSyntax < T > ( this . _binding ) ; } public toFunction ( func ) { if ( typeof func !== "function" ) { throw new Error ( ERROR_MSGS . INVALID_FUNCTION_BINDING ) ; } const bindingWhenOnSyntax = this . toConstantValue ( func ) ; this . _binding . type = BindingTypeEnum . Function ; this . _binding . scope = BindingScopeEnum . Singleton ; return bindingWhenOnSyntax ; } public toAutoFactory < T2 > ( serviceIdentifier ) { this . _binding . type = BindingTypeEnum . Factory ; this . _binding . factory = ( context ) => { const autofactory = ( ) => context . container . get < T2 > ( serviceIdentifier ) ; return autofactory ; } ; this . _binding . scope = BindingScopeEnum . Singleton ; return new BindingWhenOnSyntax < T > ( this . _binding ) ; } public toAutoNamedFactory < T2 > ( serviceIdentifier ) { this . _binding . type = BindingTypeEnum . Factory ; this . _binding . factory = ( context ) => { return ( named ) => context . container . getNamed < T2 > ( serviceIdentifier , named as string ) ; } ; return new BindingWhenOnSyntax < T > ( this . _binding ) ; } public toProvider < T2 > ( provider ) { this . _binding . type = BindingTypeEnum . Provider ; this . _binding . provider = provider ; this . _binding . scope = BindingScopeEnum . Singleton ; return new BindingWhenOnSyntax < T > ( this . _binding ) ; } public toService ( service ) { this . toDynamicValue ( ( context ) => context . container . get < T > ( service ) ) ; } } export { BindingToSyntax } ;