private function internalCollectChildrenOf(ModelInterface $model, $providerName = '', $recursive = false)
    {
        if ('' === $providerName) {
            $providerName = $model->getProviderName();
        }

        $ids = ($model->getProviderName() === $providerName) ? [$model->getId()] : [];

        // Check all data providers for children of the given element.
        $childIds = [];
        foreach ($this->relationships->getChildConditions($model->getProviderName()) as $condition) {
            $provider = $this->environment->getDataProvider($condition->getDestinationName());
            $config   = $provider->getEmptyConfig();
            $config->setFilter($condition->getFilter($model));

            foreach ($provider->fetchAll($config) as $child) {
                /** @var ModelInterface $child */

                if (!$recursive && $child->getProviderName() === $providerName) {
                    $ids[] = $child->getId();
                }

                if (false === $recursive) {
                    continue;
                }
                // Head into recursion.
                $childIds[] = $this->collectChildrenOf($child, $providerName);
            }
        }

        return \array_merge($ids, ...$childIds);
    }