import { ControllerClass } from "./controller-metadata.ts" ; import { Type } from "./deps.ts" ; import { MvTransformer , TransformerMetadata } from "./mv-transformer.ts" ; export class TransformerCatalog { private static readonly globalCatalog : TransformerMetadata [ ] = [ ] ; private static readonly catalog = new Map < Type , { actions : { [ action ] : { parameters : TransformerMetadata [ ] [ ] ; transformers : TransformerMetadata [ ] ; } ; } ; transformers : TransformerMetadata [ ] ; } > ( ) ; static registerTransformer ( transformer , type , action ? , parameterIndex ? , priority ? ) { let controllerRegistration = this . catalog . get ( type ) ; if ( ! controllerRegistration ) { controllerRegistration = { actions : { } , transformers : [ ] } ; this . catalog . set ( type , controllerRegistration ) ; } if ( ! action ) { controllerRegistration . transformers . push ( { transformer , priority } ) ; return ; } let actionRegistration = controllerRegistration . actions [ action ] ; if ( ! actionRegistration ) { actionRegistration = { parameters : [ ] , transformers : [ ] } ; controllerRegistration . actions [ action ] = actionRegistration ; } if ( parameterIndex === undefined ) { actionRegistration . transformers . push ( { transformer , priority } ) ; return ; } let parameters = actionRegistration . parameters [ parameterIndex ] ; if ( ! parameters ) { parameters = [ ] ; actionRegistration . parameters [ parameterIndex ] = parameters ; } parameters . push ( { transformer , priority } ) ; } static registerGlobalTransformer ( transformer , priority ? ) { this . globalCatalog . push ( { transformer , priority } ) ; } static getTransformers ( type , action , parameterIndex ) { const transformers = [ ... this . catalog . get ( type ) ?. transformers ?? [ ] , ... this . catalog . get ( type ) ?. actions [ action ] . transformers ?? [ ] , ... this . catalog . get ( type ) ?. actions [ action ] . parameters [ parameterIndex ] ?? [ ] , ] ; transformers . sort ( ( a , b ) => ( a ?. priority ?? 0 ) - ( b ?. priority ?? 0 ) ) ; return transformers . map ( ( t ) => t . transformer ) ; } }