import { HierarchicalFilter , isHierarchicalFilter } from '@empathyco/x-types' ; import { Store } from 'vuex' ; import { RootXStoreState } from '../../../store/store.types' ; import { addFacetIfNotPresent } from './add-facet-if-not-present' ; import { FilterEntity } from './types' ; /**
 * Allows selecting and deselecting a filter of  {@link @empathyco/x-types#HierarchicalFilter
 * | HierarchicalFilter}.
 *
 *  internal export class HierarchicalFilterEntity implements FilterEntity { public static accepts = isHierarchicalFilter ; public constructor ( protected store ) { } param filter deselect ( filter ) { this . saveFilter ( { ... filter , selected : false } ) ; this . deselectDescendants ( filter ) ; addFacetIfNotPresent ( this . store , filter . facetId , 'HierarchicalFacet' ) ; } param filter select ( filter ) { this . saveFilter ( { ... filter , selected : true } ) ; this . selectAncestors ( filter ) ; addFacetIfNotPresent ( this . store , filter . facetId , 'HierarchicalFacet' ) ; } remarks param filter internal protected deselectDescendants ( filter ) { if ( filter . children ) { filter . children . forEach ( childId => { const child = this . getFilterById ( childId ) ; if ( child ) { this . saveFilter ( { ... child , selected : false } ) ; this . deselectDescendants ( child ) ; } } ) ; } } remarks param filter internal protected selectAncestors ( filter ) { if ( filter . parentId ) { const parent = this . getFilterById ( filter . parentId ) ; if ( parent ) { this . saveFilter ( { ... parent , selected : true } ) ; this . selectAncestors ( parent ) ; } } } param id returns internal protected getFilterById ( id ) { return this . store . state . x . facets . filters [ id ] as HierarchicalFilter ; } param filter internal protected saveFilter ( filter ) { this . store . commit ( 'x/facets/setFilter' , filter ) ; } }