import { TranslateConfig } from "../interface/translate-config" ; import { TranslateContainerConfig } from "../interface/translate-container-config" ; import { defineProperty } from "../functions/define-property" ; import { defineAsyncProperty } from "../functions/define-async-property" ; import { overrideDestroyMethod } from "../functions/override-destroy" ; import { RxTranslateConfig } from "../interface/rx-translate-config" ; import { AsyncTranslateConfig } from "../interface/async-translate-config" ; export const translateContainer = new ( class { store : TranslateContainerConfig [ ] = new Array < TranslateContainerConfig > ( ) ; additionalStore : TranslateContainerConfig [ ] = new Array < TranslateContainerConfig > ( ) ; config : RxTranslateConfig ; componentState : { [ key ] : Function ; } = { } ; set ( instance , config ) { let translateConfig = this . store . filter ( t => t . instance == instance ) [ 0 ] ; if ( ! translateConfig ) this . store . push ( { instance : instance , config : config } ) ; else this . additionalStore . push ( { instance : instance , config : config } ) ; } get ( instance ) { let containerConfig = this . store . filter ( t => t . instance == instance ) ; return containerConfig . length > 0 ? containerConfig [ 0 ] : undefined ; } additionalGet ( instance ) { return this . additionalStore . filter ( t => t . instance == instance ) ; } getByName ( name ) { let containerConfig = this . store . filter ( t => t . config . translationName == name ) ; if ( containerConfig . length == 0 ) containerConfig = this . additionalStore . filter ( t => t . config . translationName == name ) ; return containerConfig . length > 0 ? containerConfig [ 0 ] : undefined ; } defineAsyncProperty ( instance , propertyName , config ) { let isPropertyKey = ( propertyName != undefined ) ; var model = ! isPropertyKey ? instance : instance . constructor ; defineAsyncProperty ( model , propertyName , config ) ; } defineProperty ( instance , propertyName , config ? ) { let isPropertyKey = ( propertyName != undefined ) ; var model = ! isPropertyKey ? instance : instance . constructor ; let modelName = config === undefined ? "global" : config . translationName ; defineProperty ( model , propertyName , modelName , config ? config . language : undefined , config ? config . filePath : undefined ) ; if ( modelName != "global" && propertyName ) { let instanceConfig = { instance : model , config : config } ; this . set ( model , config ) ; let count = this . store . filter ( t => t . instance == model ) . length ; count = this . additionalStore . filter ( t => t . instance == model ) . length + count ; if ( count == 1 ) overrideDestroyMethod ( model , config . translationName ) ; return instanceConfig ; } else if ( ! propertyName ) this . set ( model , config ) ; } setComponentState ( key , instance ) { this . componentState [ key ] = instance ; } getComponentState ( key ) { return this . componentState [ key ] ; } getActiveTranslations ( ) { return this . store ; } } ) ( ) ;