import { ControllerClass } from "./controller-metadata.ts" ; import { Type } from "./deps.ts" ; import { MvFilter } from "./mv-filter.ts" ; interface FilterRegistration { filter : MvFilter | Type < MvFilter > ; priority ? : number ; } export class FilterCatalog { private static readonly globalCatalog : FilterRegistration [ ] = [ ] ; private static readonly controllerCatalog = new Map < Type , { registrations : FilterRegistration [ ] ; actions : { [ action ] : { registrations : FilterRegistration [ ] ; } ; } ; } > ( ) ; static registerGlobalFilter ( filter , priority ? ) { this . globalCatalog . push ( { filter , priority } ) ; } static registerControllerFilter ( type , filter , priority ? ) { this . getControllerRegistration ( type ) . registrations . push ( { filter , priority } ) ; } static registerActionFilter ( type , action , filter , priority ? ) { this . getActionRegistration ( type , action ) . registrations . push ( { filter , priority } ) ; } static getFilters ( type , action ) { const mergedFilters = [ ... this . globalCatalog , ... ( this . controllerCatalog . get ( type ) ?. registrations ?? [ ] ) , ... ( this . controllerCatalog . get ( type ) ?. actions [ action ] ?. registrations ?? [ ] ) , ] ; const sortedFilters = mergedFilters . sort ( ( a , b ) => ( a . priority ?? 0 ) - ( b . priority ?? 0 ) ) . map ( ( registration ) => registration . filter ) ; return sortedFilters ; } private static getControllerRegistration ( type ) { let registration = this . controllerCatalog . get ( type ) ; if ( ! registration ) { registration = { actions : { } , registrations : [ ] } ; this . controllerCatalog . set ( type , registration ) ; } return registration ; } private static getActionRegistration ( type , action ) { const controllerRegistration = this . getControllerRegistration ( type ) ; let actionRegistration = controllerRegistration . actions [ action ] ; if ( ! actionRegistration ) { actionRegistration = { registrations : [ ] } ; controllerRegistration . actions [ action ] = actionRegistration ; } return actionRegistration ; } }