protected function filterNestedEmptyGroups(MenuPresenceInterface $presence)
    {
        if ($presence->type() !== MenuPresenceType::GROUP) {
            return 1;
        }

        $children = $presence->children();

        if ( ! $children || ! count($children)) {
            return 0;
        }

        $remove        = [];
        $nonGroupCount = 0;

        foreach ($children as $key => $childPresence) {

            if ( ! $this->filterNestedEmptyGroups($childPresence)) {
                $remove[] = $key;
                continue;
            }

            $nonGroupCount++;
        }

        foreach ($remove as $key) {
            unset($children[$key]);
        }

        $presence->setChildren($children);

        return $nonGroupCount;
    }