<?php

namespace App\Twig;

use App\Service\AnalyticsCryptoService;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Twig\Extension\AbstractExtension;
use Twig\Extension\GlobalsInterface;

class AnalyticsExtension extends AbstractExtension implements GlobalsInterface
{
    private string $endpointToken;

    public function __construct(
        private AnalyticsCryptoService $crypto,
        #[Autowire(env: 'SECRET_ANALYTICS')] string $analyticsSecret,
    ) {
        $this->endpointToken = substr(hash('sha256', $analyticsSecret.'_endpoint'), 0, 8);
    }

    public function getGlobals(): array
    {
        return [
            'analytics_key' => $this->crypto->getKeyForJs(),
            'analytics_endpoint' => '/t/'.$this->endpointToken,
        ];
    }
}
