<?php

namespace NoahVet\Reef\Business\Enumerator;

use NoahVet\Reef\Business\Model\Factory\ManufacturerWithTeamFactory;
use NoahVet\Reef\Business\Model\ManufacturerWithTeam;

class ManufacturerWithTeamEnumerator implements ManufacturerWithTeamEnumeratorInterface
{
    public function __construct(
        protected ManufacturerEnumeratorInterface $manufacturerEnumerator,
        protected ManufacturerWithTeamFactory $manufacturerWithTeamFactory,
    ) {
    }

    /**
     * @return \Generator<ManufacturerWithTeam>
     */
    #[\Override]
    public function enumerateAll(int $querySize = 50): \Generator
    {
        $manufacturers = [];

        foreach ($this->manufacturerEnumerator->enumerateAll([], $querySize) as $manufacturer) {
            $manufacturers[] = $manufacturer;

            if (\count($manufacturers) === $querySize) {
                foreach ($this->manufacturerWithTeamFactory->createBatch($manufacturers) as $item) {
                    yield $item;
                }
                $manufacturers = [];
            }
        }

        if (!empty($manufacturers)) {
            foreach ($this->manufacturerWithTeamFactory->createBatch($manufacturers) as $item) {
                yield $item;
            }
        }
    }
}
