<?php


namespace AutoAdmin\Bundle\BrickBuilder\Brick;


use AutoAdmin\Bundle\BrickBuilder\Brick\Handler\BrickAdminPreviewInterface;
use AutoAdmin\Bundle\BrickBuilder\Model\BrickInterface;

class BrickPreviewService
{
    private BrickHandlerManagerInterface $handlerManager;

    public function __construct(BrickHandlerManagerInterface $handlerManager)
    {
        $this->handlerManager = $handlerManager;
    }

    public function preview(BrickInterface $brick)
    {
        $handler = $this->handlerManager->getHandler($brick);

        if (!$handler instanceof BrickAdminPreviewInterface) {
            return null;
        }

        $brick = $this->handlerManager->buildSettings($brick);

        return $handler->getAdminPreview($brick);
    }
}