<?php


namespace AutoAdmin\Bundle\Seo\Generator;


use AutoAdmin\Bundle\Seo\DependencyInjection\Compiler\TagManagerInterface;
use AutoAdmin\Bundle\Seo\Entity\SeoMetadata;
use Traversable;

class GeneratorManager
{
    /**
     * @var GeneratorInterface[]
     */
    protected array $generators = [];

    public function __construct(Traversable $generators)
    {
        // aa_seo.generator
        $this->generators = array_map([$this, 'add'], iterator_to_array($generators));
    }

    public function generateSeo(
        object $subject,
        array  $fields,
        string $expectedType = GeneratorInterface::TYPE_MANUAL_REGEN
    ): bool|SeoMetadata|null
    {
        foreach ($this->generators as $generator) {
            if (
                count($fields) > 0
                && $generator->supports($subject)
                && in_array($expectedType, $generator->getTypes())
            ) {
                return $generator->generate($subject, $fields, $expectedType);
            }
        }

        return false;
    }

    public function add(GeneratorInterface $generator): GeneratorInterface
    {
        return $generator;
    }
}
