import { Facet , Filter , isFacetFilter } from '@empathyco/x-types' ; import { Store } from 'vuex' ; import { RootXStoreState } from '../../../store/store.types' ; import { EditableNumberRangeFilterEntity } from './editable-number-range-filter.entity' ; import { HierarchicalFilterEntity } from './hierarchical-filter.entity' ; import { NumberRangeFilterEntity } from './number-range-filter.entity' ; import { RawFilterEntity } from './raw-filter.entity' ; import { SimpleFilterEntity } from './simple-filter.entity' ; import { FilterEntity , FilterEntityConstructor , FilterEntityModifier } from './types' ; /**
 * Creates  FilterEntity based on the provided configs.
 *
 *  internal export class FilterEntityFactory { remarks public static instance = new FilterEntityFactory ( ) ; /**
     * The registered entities by default to be used by the Factory.
     */ protected entities : FilterEntityConstructor [ ] = [ SimpleFilterEntity , HierarchicalFilterEntity , NumberRangeFilterEntity , EditableNumberRangeFilterEntity , RawFilterEntity ] ; /**
     * The registered modifiers grouped by the facetId to be applied.
     */ protected modifiers : Record < Facet [ 'id' ] , FilterEntityModifier [ ] > = { } ; internal protected cache : Record < Facet [ 'id' ] | '__unknown-facet__' , FilterEntity > = { } ; param store - The  https that the entity will
     * use.
     *  param filter - The  {@link @empathyco/x-types#Filter | Filter} necessary to know what
     *  FilterEntity to create.
     *  returns The  FilterEntity created by the factory.
      getFilterEntity ( store , filter ) { const cacheKey = isFacetFilter ( filter ) ? filter . facetId : '__unknown-facet__' ; return this . cache [ cacheKey ] ?? ( this . cache [ cacheKey ] = this . createFilterEntity ( store , filter ) ) ; } /**
     * Creates a brand new  FilterEntity for the given
     *  {@link @empathyco/x-types#Filter | Filter} .
     *
     *  param store param filter returns A new  FilterEntity for the given  {@link @empathyco/x-types#Filter | Filter} .
     *  internal protected createFilterEntity ( store , filter ) { const filterEntityConstructor = this . entities . find ( entity => entity . accepts ( filter ) ) ; if ( ! filterEntityConstructor ) { throw new Error ( `Entity configuration for ${ filter . modelName } not registered.` ) ; } const entity = new filterEntityConstructor ( store ) ; const modifiers = isFacetFilter ( filter ) ? this . modifiers [ filter . facetId ] ?? [ ] : [ ] ; return modifiers . reduce ( ( modifiedEntity , modifier ) => new modifier ( store , modifiedEntity ) , entity ) ; } /**
     * Registers a new  FilterEntity to be used by the factory.
     *
     *  param entity - The new  FilterEntity to be registered in the factory.
      registerFilterEntity ( entity ) { if ( ! this . entities . includes ( entity ) ) { this . entities . push ( entity ) ; } } /**
     * Registers a list of modifiers to be used with the  FilterEntity of a
     * particular facet.
     *
     *  param facetId param modifiers registerFilterModifier ( facetId , modifiers ) { if ( ! this . modifiers [ facetId ] ) { this . modifiers [ facetId ] = [ ] ; } const facetModifiers = this . modifiers [ facetId ] ; const newModifiers = modifiers . filter ( modifier => ! facetModifiers . includes ( modifier ) ) ; facetModifiers . push ( ... newModifiers ) ; } }