import { ICategory , IScript } from '@/domain/ICategory' ; import { ICategoryCollection } from '@/domain/ICategoryCollection' ; import { INode , NodeType } from './SelectableTree/Node/INode' ; export function parseAllCategories ( collection ) { return createCategoryNodes ( collection . actions ) ; } export function parseSingleCategory ( categoryId , collection ) { const category = collection . findCategory ( categoryId ) ; if ( ! category ) { throw new Error ( `Category with id ${ categoryId } does not exist` ) ; } const tree = parseCategoryRecursively ( category ) ; return tree ; } export function getScriptNodeId ( script ) { return script . id ; } export function getScriptId ( nodeId ) { return nodeId ; } export function getCategoryId ( nodeId ) { return + nodeId ; } export function getCategoryNodeId ( category ) { return `${ category . id }` ; } function parseCategoryRecursively ( parentCategory ) { if ( ! parentCategory ) { throw new Error ( 'parentCategory is undefined' ) ; } return [ ... createCategoryNodes ( parentCategory . subCategories ) , ... createScriptNodes ( parentCategory . scripts ) , ] ; } function createScriptNodes ( scripts ) { return ( scripts || [ ] ) . map ( ( script ) => convertScriptToNode ( script ) ) ; } function createCategoryNodes ( categories ) { return ( categories || [ ] ) . map ( ( category ) => ( { category , children : parseCategoryRecursively ( category ) } ) ) . map ( ( data ) => convertCategoryToNode ( data . category , data . children ) ) ; } function convertCategoryToNode ( category , children ) { return { id : getCategoryNodeId ( category ) , type : NodeType . Category , text : category . name , children , documentationUrls : category . documentationUrls , isReversible : children && children . every ( ( child ) => child . isReversible ) , } ; } function convertScriptToNode ( script ) { return { id : getScriptNodeId ( script ) , type : NodeType . Script , text : script . name , children : undefined , documentationUrls : script . documentationUrls , isReversible : script . canRevert ( ) , } ; }