import WorkspaceAdaptor from './adaptors/workspace' ; import { LoadedNormalisationRule , SavedNormalisationRule } from './types/normalisation-rule' ; const isEqual = require ( 'lodash.isequal' ) ; const omit = require ( 'lodash.omit' ) ; const clone = ( value ) => JSON . parse ( JSON . stringify ( value ) ) ; export default class NormalisationRuleStore { private baseRules ? : SavedNormalisationRule [ ] ; private rules ? : LoadedNormalisationRule [ ] ; constructor ( private readonly workspace ) { this . setupRules ( this . workspace . get < SavedNormalisationRule [ ] > ( 'preComparisonTextNormalizationRules' ) ) ; } private setupRules ( rules ) { this . baseRules = clone ( rules ) ; this . rules = this . resetRuleStatus ( this . baseRules ! ) ; } private resetRuleStatus ( rules ) { return rules . map ( rule => Object . assign ( { } , omit ( rule , [ 'enableOnStart' ] ) , { active : rule . enableOnStart !== false } ) ) ; } getAllRules ( ) { const newBaseRules = this . workspace . get < SavedNormalisationRule [ ] > ( 'preComparisonTextNormalizationRules' ) ; if ( ! isEqual ( newBaseRules , this . baseRules ) ) { this . setupRules ( newBaseRules ) ; } return this . rules ! ; } get activeRules ( ) : LoadedNormalisationRule [ ] { return this . getAllRules ( ) . filter ( rule => rule . active ) ; } get hasActiveRules ( ) : boolean { return this . activeRules . length > 0 ; } specifyActiveRules ( ruleIndices ) { this . rules = this . rules ! . map ( ( rule , index ) => Object . assign ( { } , rule , { active : ruleIndices . includes ( index ) } ) ) ; } }