export default class MenuBuilder {

    constructor({ menuRegistry, acl }) {
        this.menuRegistry = menuRegistry;
        this.acl = acl;
    }

    build() {

        const items = this.menuRegistry.getAll();

        return items
            .filter(item => this.acl.can(item.permission))
            .map(item => ({
                ...item,
                children: (item.children || []).filter(
                    c => this.acl.can(c.permission)
                )
            }))
            .filter(item =>
                item.children.length > 0 || !item.children
            );
    }

}
