import { Facet , Filter , isFacetFilter } from '@empathyco/x-types' ; import { Store } from 'vuex' ; import { XPlugin } from '../../../plugins/index' ; import { RootXStoreState } from '../../../store/index' ; import { arrayToObject , groupItemsBy , isArrayEmpty } from '../../../utils/index' ; import { FilterEntityFactory } from '../entities/filter-entity.factory' ; import { FilterEntity } from '../entities/types' ; import { FacetGroupEntry , FacetsGetters } from '../store/types' ; import { FacetsGroup , FacetsService } from './types' ; /**
 * Default implementation for the  FacetsService .
 *
 *  public export class DefaultFacetsService implements FacetsService { /**
     * Global instance of the  FacetsService .
      public static instance : FacetsService = new DefaultFacetsService ( ) ; public constructor ( protected filterEntityFactory = FilterEntityFactory . instance ) { } /**
     * The  https store to use in the service.
     *
     *  returns internal protected get store ( ) : Store < RootXStoreState > { return XPlugin . store ; } setFacets ( facetsGroup ) { const newFilters = this . updateStore ( facetsGroup ) ; this . updateFiltersSelectedState ( newFilters ) ; } updateFacets ( facetsGroup ) { const selectedFilters = this . getSelectedFilters ( ) ; const newFilters = this . updateStore ( facetsGroup ) ; this . updateFiltersSelectedState ( newFilters , selectedFilters ) ; } clearFilters ( facetIds ? ) { this . getSelectedFilters ( ) . filter ( filter => ! facetIds || ( isFacetFilter ( filter ) && facetIds . includes ( filter . facetId ) ) ) . forEach ( this . deselect . bind ( this ) ) ; } deselect ( filter ) { this . getFilterEntity ( filter ) . deselect ( filter ) ; } select ( filterOrFilters ) { const filters = Array . isArray ( filterOrFilters ) ? filterOrFilters : [ filterOrFilters ] ; filters . forEach ( filter => this . getFilterEntity ( filter ) . select ( filter ) ) ; } toggle ( filter ) { if ( filter . selected ) { this . deselect ( filter ) ; } else { this . select ( filter ) ; } } param filter returns internal protected getFilterEntity ( filter ) { return this . filterEntityFactory . getFilterEntity ( this . store , filter ) ; } param facetsGroup - The  FacetsGroup to set into the store state.
     *  returns internal protected updateStore ( facetsGroup ) { this . removeGroupFacets ( facetsGroup . id ) ; this . removeGroupFilters ( facetsGroup . id ) ; facetsGroup . facets . forEach ( facet => { this . setFacetGroup ( { facetId : facet . id , groupId : facetsGroup . id } ) ; this . setFacet ( facet ) ; } ) ; const newFilters = facetsGroup . facets . flatMap ( facet => facet . filters ) ; this . setFilters ( newFilters ) ; return newFilters ; } returns internal protected getSelectedFilters ( ) { return this . store . getters [ 'x/facets/selectedFilters' ] ; } param newFilters param previousFilters protected updateFiltersSelectedState ( newFilters , previousFilters ? ) { if ( ! isArrayEmpty ( newFilters ) ) { const newStateFiltersMap = arrayToObject ( previousFilters ?? newFilters , 'id' ) ; newFilters . forEach ( filter => { const filterEntity = this . getFilterEntity ( filter ) ; if ( newStateFiltersMap [ filter . id ] ?. selected ) { filterEntity . select ( filter ) ; } else { filterEntity . deselect ( filter ) ; } } ) ; } } param groupId returns internal protected removeGroupFilters ( groupId ) { const filtersToRemove = groupItemsBy ( Object . values ( this . store . state . x . facets . filters ) , filter => isFacetFilter ( filter ) ? this . store . state . x . facets . groups [ filter . facetId ] : '__unknown-group__' ) [ groupId ] ?? [ ] ; this . removeFilters ( filtersToRemove ) ; return filtersToRemove ; } param groupId returns internal protected removeGroupFacets ( groupId ) { const facetsToRemove = Object . values ( this . store . state . x . facets . facets ) . filter ( facet => this . store . state . x . facets . groups [ facet . id ] === groupId ) ; facetsToRemove . forEach ( this . removeFacet . bind ( this ) ) ; return facetsToRemove ; } param facetGroup internal protected setFacetGroup ( facetGroup ) { this . store . commit ( 'x/facets/setFacetGroup' , facetGroup ) ; } param facet internal protected setFacet ( { filters , ... restFacet } ) { this . store . commit ( 'x/facets/setFacet' , restFacet ) ; } param facet internal protected removeFacet ( facet ) { this . store . commit ( 'x/facets/removeFacet' , facet ) ; } param filters internal protected setFilters ( filters ) { this . store . commit ( 'x/facets/setFilters' , filters ) ; } param filters internal protected removeFilters ( filters ) { this . store . commit ( 'x/facets/removeFilters' , filters ) ; } }