<?php


namespace AutoAdmin\Bundle\Seo\Generator;


use AutoAdmin\Bundle\Seo\Entity\SeoMetadata;

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

    public function __construct(array $generators = [])
    {
        array_map([$this, 'addGenerator'], $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;
    }

    private function addGenerator(GeneratorInterface $generator): void
    {
        $this->generators[] = $generator;
    }
}
