<?php
namespace App\Services;

final class CommunicationTemplateRenderer
{
    public function render(
        string $content,
        array $context
    ): string {
        $replacements=[];

        foreach($context as $key=>$value){
            $replacements['{{'.$key.'}}']=
                is_scalar($value)
                    ? (string)$value
                    : '';
        }

        return strtr($content,$replacements);
    }

    public function unresolvedVariables(
        string $content
    ): array {
        preg_match_all(
            '/\{\{\s*([a-zA-Z0-9_]+)\s*\}\}/',
            $content,
            $matches
        );

        return array_values(
            array_unique($matches[1] ?? [])
        );
    }
}
