import { SelectedScript } from '@/application/Context/State/Selection/SelectedScript' ; import { IUserSelection } from '@/application/Context/State/Selection/IUserSelection' ; import { ICategoryCollection } from '@/domain/ICategoryCollection' ; import { getCategoryId } from '../../../ScriptNodeParser' ; import { IReverter } from './IReverter' ; import { ScriptReverter } from './ScriptReverter' ; export class CategoryReverter implements IReverter { private readonly categoryId : number ; private readonly scriptReverters : ReadonlyArray < ScriptReverter > ; constructor ( nodeId , collection ) { this . categoryId = getCategoryId ( nodeId ) ; this . scriptReverters = getAllSubScriptReverters ( this . categoryId , collection ) ; } public getState ( selectedScripts ) { return this . scriptReverters . every ( ( script ) => script . getState ( selectedScripts ) ) ; } public selectWithRevertState ( newState , selection ) { selection . addOrUpdateAllInCategory ( this . categoryId , newState ) ; } } function getAllSubScriptReverters ( categoryId , collection ) { const category = collection . findCategory ( categoryId ) ; if ( ! category ) { throw new Error ( `Category with id "${ categoryId }" does not exist` ) ; } const scripts = category . getAllScriptsRecursively ( ) ; return scripts . map ( ( script ) => new ScriptReverter ( script . id ) ) ; }