<?php


namespace AutoAdmin\Bundle\Seo\Generator;


use AutoAdmin\Bundle\Content\Model\NameInterface;
use AutoAdmin\Bundle\Content\Model\TitleInterface;
use AutoAdmin\Bundle\Seo\Entity\SeoMetadata;

class DefaultGenerator extends AbstractGenerator
{
    public function supports(object $subject): bool
    {
        return true;
    }

    public function generate(object $subject, array $fields, string $type): ?SeoMetadata
    {
        $subject = $this->getSeoHolder($subject);

        $seo = $subject->getSeoMetadata();

        if ($seo && $subject instanceof TitleInterface && in_array('title', $fields)) {
            $seo->setTitle($subject->getTitle());
        }

        if ($seo && $subject instanceof NameInterface && in_array('title', $fields)) {
            $seo->setTitle($subject->getName());
        }

        return $seo;
    }
}