<?php

namespace App\Service\SnipParser\Twig;

use App\Service\SnipParser\AbstractParser;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
use Twig\Loader\ChainLoader;

class TwigParser extends AbstractParser
{
    public function __construct(
        private readonly SnipLoader $snipLoader,
        private readonly SnipTwigExtension $snipTwigExtension,
    ) {}

    public function safeParseView(string $content): string
    {
        $loader = new ChainLoader([
            new ArrayLoader([
                'index' => $content,
            ]),
            $this->snipLoader,
        ]);
        $twig = new Environment($loader);
        $twig->addExtension($this->snipTwigExtension);

        return $twig->render('index');
    }
}