<?php

namespace AutoAdmin\Bundle\Media\Library;

use Twig\Environment;

class PreviewGenerator
{
    public function __construct(
        protected MediaLibrary $library,
        protected MediaPathGenerator $urlGenerator,
        protected Environment $twig,
    ) {
    }

    public function getPreview(string $type, int $id): ?string
    {
        $item = $this->library->get($type, $id);

        if (empty($item)) {
            return null;
        }

        return $this->twig->render('@AutoAdminMedia/Form/linked_media_preview.html.twig', [
            'currentMedia' => $item,
            'currentMediaUrl' => $this->urlGenerator->generateWebPath($item),
            'mediaType' => $type,
        ]);
    }

    public function getEmptyPreview(bool $unlinked = false): ?string
    {
        return $this->twig->render('@AutoAdminMedia/Form/linked_media_preview_empty.html.twig', [
            'unlinked' => $unlinked,
        ]);
    }
}
