import { CollectionItem , CollectionNode , isItem , isSection } from "./useCollection" ; export function reduceCollection ( nodes , acceptItem ) { return nodes . reduce ( ( acc , node ) => { if ( isSection ( node ) ) { const items = node . items . reduce ( ( sectionItems , item ) => { if ( acceptItem ( item ) ) { sectionItems . push ( item ) ; } return sectionItems ; } , [ ] ) ; if ( items . length > 0 ) { const { items : _ , ... sectionProps } = node ; acc . push ( { ... sectionProps , items } ) ; } } else if ( isItem ( node ) ) { if ( acceptItem ( node ) ) { acc . push ( node ) ; } } else { acc . push ( node ) ; } return acc ; } , [ ] ) ; }