<?php

namespace NoahVet\Reef\Security\IAM\Finder;

use NoahVet\Reef\Domain\Security\Port\ResourceEnumeratorInterface;
use NoahVet\Reef\Domain\Security\Port\ResourceFinderInterface;

class ResourceIterator
{
    /**
     * @var iterable<ResourceFinderInterface>
     */
    protected iterable $iterators;

    /**
     * @param iterable<ResourceFinderInterface> $iterators
     */
    public function __construct(
        iterable $iterators,
    ) {
        $this->iterators = $iterators;
    }

    /**
     * @return \Generator<mixed>
     */
    public function iterate(): \Generator
    {
        foreach ($this->iterators as $iterator) {
            if (!$iterator instanceof ResourceEnumeratorInterface) {
                continue;
            }

            foreach ($iterator->enumerate() as $item) {
                yield $item;
            }
        }
    }
}
